简体   繁体   中英

c# windows forms multithreading updating form controls from another class event

onrecvdChanged is an event (of Server Class) once occured I want it to change the text of label1 of the form1 class. I've been stuck here for a long time. I tried a few tricks but I get a cross thread exception.

namespace TCPServerTutorial
{
    public partial class Form1 : Form
    {
        Server a;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Ready";
            a=new Server(this);
        }
        private void Start_btn_Click(object sender, EventArgs e)
        {
            a.StartTcpServer();
            label1.Text = "Listening...";
        }

        private void Stop_Btn_Click(object sender, EventArgs e)
        {
            a.StopListenForClients();
            label1.Text = "Stopped...";
        }
    }



    class Server
    {
        public event EventHandler recvdChanged;
        private TcpListener tcpListener;
        private Thread listenThread;
        private string recvd;
        Form1 _f1parent;
        public Server(Form1 par)
        {
            _f1parent = par;
        }
        public string getsetrecvd
        {
            get { return this.recvd; }
            set
            {
                this.recvd = value;
                if (this.recvdChanged != null)
                    this.recvdChanged(this, new EventArgs());
            }
        }
        public void StartTcpServer()
        {
            this.tcpListener = new TcpListener(IPAddress.Any, 3000);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }
        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
                if(client.Connected)
                {
                    MessageBox.Show(client.Client.RemoteEndPoint + " Has Connected.");
                }

                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }
        public void StopListenForClients()
        {
            tcpListener.Stop();
        }
        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            recvdChanged += new EventHandler(onrecvdChanged);
            byte[] message = new byte[4096];
            int bytesRead;
            Form1 p = new Form1();
            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                getsetrecvd=encoder.GetString(message, 0, bytesRead);
                if (recvd != "e")
                {
                }
                else
                {
                    break;
                }
            }

            tcpClient.Close();
        }
        void onrecvdChanged(object sender, EventArgs e)
        {
            MessageBox.Show("event: " + recvd);
        }
    }
}

You already have an event that is fired when the string is changed, recvdChanged . Just have the form add an event handler to that event and then invoke to the UI thread:

public Form1()
{
    InitializeComponent();
    label1.Text = "Ready";
    a=new Server(this);
    a.recvdChanged += (_,args) => Invoke(new MethodInvoker(
        ()=>label1.Text = a.getsetrecvd));
}

There is no need for the event handler for that event inside of the Server class itself.

A cross thread exception is usually fixed by the use of Invoke.

I find the simplest way is to use a MethodInvoker cast and a Lambda expression...something like:

Invoke((MethodInvoker)( () => Form1.Label1.Text = "some string" ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM