Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确关闭线程中打开的TCP客户端_C#_Multithreading_Sockets_Tcp_Tcpclient - Fatal编程技术网

C# 如何正确关闭线程中打开的TCP客户端

C# 如何正确关闭线程中打开的TCP客户端,c#,multithreading,sockets,tcp,tcpclient,C#,Multithreading,Sockets,Tcp,Tcpclient,我通过线程连接到TcpClient。现在,TcpClient的连接在线程中正确打开,但TcpClient的关闭和线程未正确发生。我不知道为什么 以下是我要开始的主题: private System.Threading.Thread _thread; private ManualResetEvent _shutdownEvent = new ManualResetEvent(false); _thread = new Thread(DoWork); _thread.Start(); 下面

我通过
线程连接到
TcpClient
。现在,
TcpClient
的连接在线程中正确打开,但
TcpClient
的关闭和线程未正确发生。我不知道为什么

以下是我要开始的主题:

 private System.Threading.Thread _thread;
 private ManualResetEvent _shutdownEvent = new ManualResetEvent(false); 

_thread = new Thread(DoWork);
_thread.Start();
下面是
TcpClient
连接:

private void DoWork()
{
while (!_shutdownEvent.WaitOne(0))
{                           
    try
    {
        client = new TcpClient();
        client.Connect(new IPEndPoint(IPAddress.Parse(ip),intport));
        //Say thread to sleep for 1 secs.
        Thread.Sleep(1000);
    }
    catch (Exception ex)
    {
        // Log the error here.
        client.Close();
        continue;
    }
    try
    {
        using (stream = client.GetStream())
        {

           byte[] notify = Encoding.ASCII.GetBytes("Hello");
           stream.Write(notify, 0, notify.Length);

            byte[] data = new byte[1024];
            while (!_shutdownEvent.WaitOne(0))
            {
                int numBytesRead = stream.Read(data, 0, data.Length);
                if (numBytesRead > 0)
                {
                    line= Encoding.ASCII.GetString(data, 0, numBytesRead);
                }
            }
            ...
下面是关闭并重新启动线程和
TcpClient
的代码:

 _shutdownEvent.WaitOne(0);
 _thread.Abort();

 //Start Again
 _thread = new Thread(DoWork);
 _thread.Start();
请帮助我正确地停止和启动线程和
TcpClient

谢谢。

将关闭事件声明为自动恢复事件

AutoResetEvent _shutdownEvent = new AutoResetEvent(false);
在定位销方法中拆下外部while环

private void DoWork()
    {
        try
        {
            client = new TcpClient();
            client.Connect(new IPEndPoint(IPAddress.Parse(ip), intport));
            //Say thread to sleep for 1 secs.
            Thread.Sleep(1000);
        }
        catch (Exception ex)
        {
            // Log the error here.
            client.Close();
            return;
        }
        try
        {
            using (stream = client.GetStream())
            {
                byte[] notify = Encoding.ASCII.GetBytes("Hello");
                stream.Write(notify, 0, notify.Length);

                byte[] data = new byte[1024];
                while (!_shutdownEvent.WaitOne(0))
                {
                    int numBytesRead = stream.Read(data, 0, data.Length);
                    if (numBytesRead > 0)
                    {
                        line = Encoding.ASCII.GetString(data, 0, numBytesRead);
                    }
                }
            }
        }
        catch
        {
        }
        finally
        {
            if (stream != null)
                stream.Close();
            if (client != null)
                client.Close();
        }        
    }
使用
\u shutdownEvent.Set()
发出事件信号

_shutdownEvent.Set(); //Set, not Wait
//_thread.Abort(); //and you don't have to abort the thread since it will end gracefully after it break out of the while loop
while (_thread.IsAlive) ; //wait until the first thread exit

//Start Again
_thread = new Thread(DoWork);
_thread.Start();

我无法重新启动
TcpClient()具有给定代码的客户端
\u线程=新线程(DoWork)_thread.Start()使用此代码,我无法在Dowork()方法中获取断点..@HansPassant您能告诉我正确的方法吗。。Thnks@HansPassant由于ManualResetEvent的状态在成功等待后不会重置为无信号状态。所以我们需要一个自动resetEvent。但是我无法再次启动线程。
\u Thread=new Thread(DoWork)_thread.Start()