为什么我的对象被另一个线程使用?

时间:2016-10-22 22:57:43

标签: c# multithreading sockets tcp

当我按下TCP客户端的发送按钮时,它会停止TCP服务器并将此错误抛给我。

  

调用线程无法访问此对象,因为它不同   线程拥有它。

我尝试过调试但无法找到问题。 我怎么能解决这个问题,因为它给我带来了很多麻烦,我不熟悉TCP / IP和线程。 我知道它是如何工作的但是是的。

在listenerThread()方法中,这一行给我带来了这个错误。

lbConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");

using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Text;


namespace SimpleTCPServer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ArrayList nSockets;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
            lblStatus.Content = "My IP address is " + IPHost.AddressList[0].ToString();
            nSockets = new ArrayList();
            Thread thdListener = new Thread(new ThreadStart(listenerThread));
            thdListener.Start();
        }


        public void listenerThread()
        {
            TcpListener tcpListener = new TcpListener(8080);
            tcpListener.Start();
            while (true)
            {
                Socket handlerSocket = tcpListener.AcceptSocket();
                if (handlerSocket.Connected)
                {
                    Control.CheckForIllegalCrossThreadCalls = false;
                    lbConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.");
                    lock (this)
                    {
                        nSockets.Add(handlerSocket);
                    }
                    ThreadStart thdstHandler = new
                    ThreadStart(handlerThread);
                    Thread thdHandler = new Thread(thdstHandler);
                    thdHandler.Start();
                }
            }
        }

        public void handlerThread()
        {
            Socket handlerSocket = (Socket)nSockets[nSockets.Count - 1];
            NetworkStream networkStream = new NetworkStream(handlerSocket);
            int thisRead = 0;
            int blockSize = 1024;
            Byte[] dataByte = new Byte[blockSize];
            lock (this)
            {
                // Only one process can access
                // the same file at any given time
                Stream fileStream = File.OpenWrite("c:\\my documents\\SubmittedFile.txt");
                while (true)
                {
                    thisRead = networkStream.Read(dataByte, 0, blockSize);
                    fileStream.Write(dataByte, 0, thisRead);
                    if (thisRead == 0) break;
                }
                fileStream.Close();
            }
            lbConnections.Items.Add("File Written");
            handlerSocket = null;
        }

    }
}

TCP客户端

using Microsoft.Win32;
using System.IO;
using System.Net.Sockets;
using System.Windows;
using System.Threading;
using System.Net;
using System.Text;
namespace SimpleTCPClient

{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
                fileTextbox.Text = (openFileDialog.FileName);

        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            Stream fileStream = File.OpenRead(fileTextbox.Text);
            // Alocate memory space for the file
            byte[] fileBuffer = new byte[fileStream.Length];
            fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
            // Open a TCP/IP Connection and send the data
            TcpClient clientSocket = new TcpClient(ipTextbox.Text, 8080);
            NetworkStream networkStream = clientSocket.GetStream();
            networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0));
            networkStream.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果lbConnections是客户端控件,则无法从后台线程修改它 - 您必须调用将在GUI线程上进行更新的方法。 GUI控件实际上没有线程安全性,并且所有禁用Control.CheckForIllegalCrossThreadCalls的做法都会让你在脚下射击时不那么明显。

一般来说,您还有其他问题。例如,你的处理程序线程总是试图抓住最后一个连接 - 在任何一个处理程序线程尝试获取套接字之前,很可能会发生两个连接(并且都被添加到nSockets) - 这意味着一个套接字永远不会被抓取一个人被抓了两次。另外,看起来你从未实际处理任何套接字对象,甚至不允许它们是GCd,因为我没有看到任何从nSockets中删除的代码。

你真的有两个不错的选择 - 要么将处理程序线程传递给它需要的数据,要么使用类似队列的东西,让处理程序线程将一个套接字出列并对其进行操作(当然所有队列访问都必须同步)。无论哪种方式,你应该处理插座而不是永远保持它们。