Thread.Start是不是在某些情况下,稀疏返回我的C#应用​​程序 [英] Thread.Start is not returning in some sparse cases in my c# application

查看:141
本文介绍了Thread.Start是不是在某些情况下,稀疏返回我的C#应用​​程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C#中的TCP服务器应用程序。应用程序监听使用TcpListener.AcceptTcpClient主侦听器线程()方法的入站连接。





当连接被接收,TcpListener.AcceptTcpClient()解封并返回的TcpClient对象。



在接收到连接,一个新的线程创建并启动读取写入数据到新的连接。



新的线程是由下面的代码启动。

 ,而(真)
{
TcpClient的客户端= serverListener.AcceptTcpClient();


如果(client.Connected)
{
线程t =新主题(委托(){readWriteData(客户端);});
t.IsBackground = TRUE;
t.Start(); ///问题发生在这里。该线程被困在这里,不动进一步
}
}



该应用程序运行正常,但在Windows 7计算机有时,应用程序突然停止监听TCP连接。



在这种状态下的应用程序的线程堆栈的分析,(微软堆栈资源管理器是用来查看应用程序的所有线程的堆栈)时,发现主监听线程粘在下面一行

 上方

所示的代码段

t.Start的(); ///问题发生在这里。该线程被困在这里,不动进一步



我也做了大量的研究,但没有找到为什么它正在发生。此行为仅在Windows 7系统中观察到。



任何人可以帮我解决这个问题。



的建议罗布,



我张贴在这里通过的WinDbg(SOS)显示堆栈跟踪

  0547eae0 7282e006 mscorwks!主题:: StartThread + 0xc3,呼吁mscorwks!_EH_epilog3 
0547eb00 727ac825 mscorwks!__ SwitchToThread + 0xd中,呼吁mscorwks !__ DangerousSwitchToThread
0547eb10 728b9c6f mscorwks ThreadNative :: StartInner + 0x1ba,呼吁mscorwks __ SwitchToThread
0547eb58 727e4b04 mscorwks的SafeHandle :: DisposeNative + 0x3a,呼吁mscorwks LazyMachStateCaptureState
0547ebc8 728b9d80 mscorwks ThreadNative!!!! :启动+ 0xa6,呼吁mscorwks ThreadNative :: StartInner
0547ec18 728b9d01 mscorwks ThreadNative ::启动+ 0x1F的,呼吁mscorwks LazyMachStateCaptureState
0547ec74 71de6afc(方法描述0x71c13048 + 0x8c System.Threading.Thread.Start(!!! )),呼吁mscorwks!ThreadNative ::启动
0547ec8c 030e2a46(方法描述0x30da408 + 0x25e WindowsService.Server.startListener()),调用(方法描述0x71c13048 + 0 System.Threading.Thread.Start())


解决方案

不过我还没有找到根本原因,为什么上面提到的问题发生。然而,为了防止我的应用程序失败,因为这种情况我已经实现了以下解决方法。



修改后的代码如下所示。

 计数= 0; 

,而(真)
{
TcpClient的客户端= serverListener.AcceptTcpClient();


如果(client.Connected)
{
线程t =新主题(委托(){readWriteData(客户端);});
t.IsBackground = TRUE;

++计数;
t.Start(); ///问题发生在这里。该线程被困在这里,不动进一步
++计数;
}



}



我检查在另一个线程,如果计数值5秒没有改变,计数的值是奇数,这意味着监听线程停留在t.start()。在这种情况下,我终止当前侦听线程并开始新的。


I have written a TCP server application in c#. Application listens for inbound connections

using TcpListener.AcceptTcpClient() method in main listener thread.

When a connection is received, TcpListener.AcceptTcpClient() unblocks and returns TCPClient object.

On receiving a connection, a new thread is created and started to read write data to new connection.

The new thread is started by following code.

while(true)
{
  TcpClient client = serverListener.AcceptTcpClient();


  if (client.Connected)
  {
    Thread t = new Thread(delegate() { readWriteData(client); });
    t.IsBackground = true;
    t.Start(); /// Problem happens here. The thread gets stuck here and doesn't move   further
  }
 }

The application runs fine but in some times in Windows 7 machines, the application suddenly stops listening for tcp connections.

On analysis of thread stacks of application in this state, ( Microsoft stack explorer was used to view stacks of all threads of the application ) it is found that the main listener thread is stuck on following line of the code section shown above

 t.Start(); /// Problem happens here. The thread gets stuck here and doesn't move   further

I did lot of research and couldn't find why it is happening. This behavior is observed only in windows 7 systems.

Can anybody please help me to solve this issue.

As suggested by Rob,

I am posting here stack trace shown by windbg (sos)

0547eae0 7282e006 mscorwks!Thread::StartThread+0xc3, calling mscorwks!_EH_epilog3
0547eb00 727ac825 mscorwks!__SwitchToThread+0xd, calling mscorwks!__DangerousSwitchToThread
0547eb10 728b9c6f mscorwks!ThreadNative::StartInner+0x1ba, calling mscorwks!__SwitchToThread
0547eb58 727e4b04 mscorwks!SafeHandle::DisposeNative+0x3a, calling mscorwks!LazyMachStateCaptureState
0547ebc8 728b9d80 mscorwks!ThreadNative::Start+0xa6, calling mscorwks!ThreadNative::StartInner
0547ec18 728b9d01 mscorwks!ThreadNative::Start+0x1f, calling mscorwks!LazyMachStateCaptureState
0547ec74 71de6afc (MethodDesc 0x71c13048 +0x8c System.Threading.Thread.Start()), calling mscorwks!ThreadNative::Start
0547ec8c 030e2a46 (MethodDesc 0x30da408 +0x25e WindowsService.Server.startListener()), calling (MethodDesc 0x71c13048 +0 System.Threading.Thread.Start())

解决方案

Still I have not found the root cause why above mention problem is happening. However to prevent my application failing because of this situation I have implemented following workaround.

The modified code is as below.

count = 0;

while(true)
{
  TcpClient client = serverListener.AcceptTcpClient();


  if (client.Connected)
  {
    Thread t = new Thread(delegate() { readWriteData(client); });
    t.IsBackground = true;

    ++count;
    t.Start(); /// Problem happens here. The thread gets stuck here and doesn't move   further
    ++count;
  }

}

I check in another thread that if value of count hasn't changed in 5 secs and the value of count is odd number that means the listener thread is stuck on t.start(). In that case, I terminate the current listener thread and start new one.

这篇关于Thread.Start是不是在某些情况下,稀疏返回我的C#应用​​程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆