聊天应用程序c#安全线程

时间:2017-08-31 16:21:33

标签: c# thread-safety

我使用基于C#网络编程代码的线程创建了一个LAN聊天应用程序 - Richard Blum。但是当我运行该程序时(我打开2个窗口聊天应用程序),单击Connect in one side并突然代码崩溃并显示错误“附加信息:跨线程操作无效:控制'lst_show'从线程以外的线程访问它创建于。“

有人帮我,我在这里问几个小时,但仍然无法解决。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Chat_Threads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private static TextBox newText = new TextBox();
        private static ListBox results = new ListBox();
        private static Socket client;
        private static byte[] data = new byte[1024];


        private void btn_listen_Click(object sender, EventArgs e)
        {
            lst_show.Items.Add("Listening for a client...");

            Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);

            newsock.Bind(iep);

            newsock.Listen(5);

            newsock.BeginAccept(new AsyncCallback(AcceptConn),newsock);   


        }

        private void AcceptConn(IAsyncResult ar)
        {
            Socket oldserver = (Socket)ar.AsyncState;

            client = oldserver.EndAccept(ar);

            lst_show.Items.Add("Connection from: " + client.RemoteEndPoint.ToString());

            Thread receiver = new Thread(new ThreadStart(ReceiveData));

            receiver.Start();
        }

        private void ReceiveData()
        {
            int recv;
            string stringData;
            while (true)
            {
                recv = client.Receive(data);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                if (stringData == "bye")
                    break;
                lst_show.Items.Add(stringData);
            }
            stringData = "bye";
            byte[] message = Encoding.ASCII.GetBytes(stringData);
            client.Send(message);
            client.Close();
            lst_show.Items.Add("Connection stopped");
            return;
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            lst_show.Items.Add("Connecting...");

            client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),9050);

            client.BeginConnect(iep, new AsyncCallback(Connected),client);
        }

        private void Connected(IAsyncResult ar)
        {
            try
            {
                client.EndConnect(ar);

                this.lst_show.Items.Add("Connected to: " + client.RemoteEndPoint.ToString());

                Thread receiver = new Thread(new ThreadStart(ReceiveData));
                receiver.Start();

            }
            catch (SocketException)
            {
                this.lst_show.Items.Add("Error connecting");
            }
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            byte[] message = Encoding.ASCII.GetBytes(txt_message.Text);

            txt_message.Clear();

            client.BeginSend(message, 0, message.Length, 0, new AsyncCallback(SendData),client);

        }

        private void SendData(IAsyncResult ar)
        {
            Socket remote = (Socket)ar.AsyncState;
            int sent = remote.EndSend(ar);
        }


    }
}

2 个答案:

答案 0 :(得分:0)

大多数UI引擎都有一个主线程,其中有一个专用的处理循环来更新用户界面。这是因为,如果多个线程尝试进行更改,则可能会造成竞争条件或死锁。

您的业务逻辑通常在单独的线程上运行,尤其是当您执行TCP / IP通信等操作时。当业务逻辑需要对UI进行更改时,它会以间接方式进行更改,例如将消息放入队列或调用特殊方法。

在.NET中,您可以使用Invoke方法在主UI线程中运行代码,然后在有机会时安全地执行它。例如:

lst_show.Items.Invoke((MethodInvoker)delegate 
{
    lst_show.Items.Add("Connection from: " + client.RemoteEndPoint.ToString());
});

答案 1 :(得分:0)

调用者位于不同的线程上,因此您必须确保主UI线程上的列表和调用者,以便以下是

需要

control.Invoke

     delegate void UpdateStringinTheGrid(string s);

      private void ListAddItem(string s)
        {
            if (lst_show.InvokeRequired())
            {
                var updateDel = new UpdateStringinTheGrid(ListAddItem);
                this.BeginInvoke(updateDel, s);
            }
          else
            lst_show.Items.Add(s);

}
    private void ReceiveData()
            {
                int recv;
                string stringData;
                while (true)
                {
                    recv = client.Receive(data);
                    stringData = Encoding.ASCII.GetString(data, 0, recv);
                    if (stringData == "bye")
                        break;
                   listAddString(stringData);
                }
                stringData = "bye";
                byte[] message = Encoding.ASCII.GetBytes(stringData);
                client.Send(message);
                client.Close();
                lst_show.Items.Add("Connection stopped");
                return;
            }