u010358801
D_galaxy
2017-11-20 01:54

新手向:关于Dispatcher 引用的问题

5
  • c#
  • 线程
  • dispatcher

如上所示:
vs2015 显示的错误信息如下

错误 CS1061 “Form1”未包含“Dispatcher”的定义,并且找不到可接受第一个“Form1”类型参数的扩展方法“Dispatcher”(是否缺少 using 指令或程序
集)

代码如下所示:
using System;
using System.IO;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;

namespace Commuicate_With_Plc
{
public partial class Form1 : Form
{
private static NetworkStream clientStream;
public BinaryWriter bw;

    public TcpClient chatClient;
    public StreamWriter writeStream;
    private StreamReader readStream;
    //Delegate int DelMath(int a, int b);
    public Form1()
    {
        InitializeComponent();
        Thread t = new Thread(WorkThread);
        t.IsBackground = true;
        t.Start();
    }
    public void connectCmd()
    {
        //Connect to the server on the IP and Port

        if (clientStream == null)
        {
            try
            {
                chatClient = new TcpClient(plcip.Text, Convert.ToInt32(port.Text));
                clientStream = chatClient.GetStream();
                StreamWriter writeStream = new StreamWriter(clientStream);
                StreamReader readStream = new StreamReader(clientStream);
                Receive.Text = "建立连接成功";


            }
            catch(Exception ex)
            {
                Receive.Text = "建立连接失败";
                MessageBox.Show(ex.ToString());
            }


        }


    }
    void WorkThread()
    {
        string receive = null;
        while (true)
        {


            receive = readStream.ReadLine();
            this.Dispatcher.Invoke(() => textBox.Text += receive + "\r\n");

            // 后台线程停顿1秒    
            Thread.Sleep(1000);
        }
    }
    private String GetCommand()
    {
        if (Receive.InvokeRequired)
        {
            return (String)Receive.Invoke(new Func<String>(() => { return Receive.Text; }));
        }
        else
        {
            return Receive.Text;
        }
    }




    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
         connectCmd();
    }

    private void plcip_TextChanged(object sender, EventArgs e)
    {

    }

    private void Send_Click(object sender, EventArgs e)
    {

        string receive = null;
        try
        {
            NetworkStream clientStream = chatClient.GetStream();
            StreamWriter writeStream = new StreamWriter(clientStream);
            StreamReader readStream = new StreamReader(clientStream);
            writeStream.Write(textBox2.Text);
            Receive.Text += textBox2.Text + "\r\n";
            GetCommand();

            // writeStream.WriteLine(cmd);
            // writeStream.Flush();



        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }


    }

    private void port_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void Receive_TextChanged(object sender, EventArgs e)
    {

    }

    private void Disconnect_Click(object sender, EventArgs e)
    {
        if(chatClient!= null)
        {
            try
            {
                chatClient.Close();
                chatClient = null;

                Receive.Text = "断开连接成功!";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换