什么是一个好办法阻塞NamedPipeServer#为waitForConnection关机线程? [英] What is a good way to shutdown Threads blocked on NamedPipeServer#WaitForConnection?

查看:326
本文介绍了什么是一个好办法阻塞NamedPipeServer#为waitForConnection关机线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了我的应用程序,它会生成多个线程,每个创建NamedPipeServer(.Net 3.5中管理类型的命名管道IPC)并等待客户端连接(块)。在code发挥预期的作用。

 私人无效StartNamedPipeServer()
  {
    使用(NamedPipeServerStream pipeStream =
                    新NamedPipeServerStream(m_sPipeName,PipeDirection.InOut,m_iMaxInstancesToCreate,PipeTransmissionMode.Message,PipeOptions.None))
    {
      m_pipeServers.Add(pipeStream);
      而(!m_bShutdownRequested)
      {
        pipeStream.WaitForConnection();
        Console.WriteLine(收到客户端的连接由{0},Thread.CurrentThread.Name);
        ....
 

现在我还需要一个关机方法,使这个过程倒干净。我想一般的布尔标志isShutdownRequested把戏。但pipestream保持阻塞在waitForConnection()调用和线程不会死。

 公共无效停止()
{
   m_bShutdownRequested = TRUE;
   的for(int i = 0; I< m_iMaxInstancesToCreate;我++)
   {
     线程t = m_serverThreads [I]
     NamedPipeServerStream pipeStream = m_pipeServers [I]
     如果(pipeStream!= NULL)
     {
       如果(pipeStream.IsConnected)
          pipeStream.Disconnect();
       pipeStream.Close();
       pipeStream.Dispose();
     }

     Console.Write(关闭{0} ...,t.Name);
     t.Join();
     Console.WriteLine(完成了!);
   }
}
 

加入从不返回。

这是我没有尝试,但可能会工作是调用Thread.Abort的,吃起来异常的选项。但它并不适合我。任何建议

更新2009-12-22
对不起,没有张贴这早些时候..这是我收到的来自金汉密尔顿(BCL团队)

回复
  

正确的方式做一个中断   为waitForConnection是调用   BeginWaitForConnection,处理新   在回调的连接,并关闭   管流停止等待   连接。如果管道是关闭的,   EndWaitForConnection将抛出   的ObjectDisposedException其中   回调线程可以赶上,清理   任何有始有终,而退出的。

     

我们意识到这必须是一个常见的   的问题,所以有人在我的球队是   计划在博客上写下这样很快。

解决方案

切换到异步版本: BeginWaitForConnection

如果它曾经完成后,你需要一个标志,从而完成处理程序只需调用 EndWaitForConnection 吸收任何异常和退出(呼叫结束......确保任何资源能够被清理)。

I start my application which spawns a number of Threads, each of which creates a NamedPipeServer (.net 3.5 added managed types for Named Pipe IPC) and waits for clients to connect (Blocks). The code functions as intended.

private void StartNamedPipeServer()
  {
    using (NamedPipeServerStream pipeStream =
                    new NamedPipeServerStream(m_sPipeName, PipeDirection.InOut, m_iMaxInstancesToCreate, PipeTransmissionMode.Message, PipeOptions.None))
    {
      m_pipeServers.Add(pipeStream);
      while (!m_bShutdownRequested)
      {
        pipeStream.WaitForConnection();
        Console.WriteLine("Client connection received by {0}", Thread.CurrentThread.Name);
        ....

Now I also need a Shutdown method to bring this process down cleanly. I tried the usual bool flag isShutdownRequested trick. But the pipestream stays blocked on the WaitForConnection() call and the thread doesn't die.

public void Stop()
{
   m_bShutdownRequested = true;
   for (int i = 0; i < m_iMaxInstancesToCreate; i++)
   {
     Thread t = m_serverThreads[i];
     NamedPipeServerStream pipeStream = m_pipeServers[i];
     if (pipeStream != null)
     {
       if (pipeStream.IsConnected)
          pipeStream.Disconnect();
       pipeStream.Close();
       pipeStream.Dispose();
     }

     Console.Write("Shutting down {0} ...", t.Name);
     t.Join();
     Console.WriteLine(" done!");
   }
}

Join never returns.

An option that I didnt try but would possibly work is to call Thread.Abort and eat up the exception. But it doesn't feel right.. Any suggestions

Update 2009-12-22
Sorry for not posting this earlier.. This is what I received as a response from Kim Hamilton (BCL team)

The "right" way to do an interruptible WaitForConnection is to call BeginWaitForConnection, handle the new connection in the callback, and close the pipe stream to stop waiting for connections. If the pipe is closed, EndWaitForConnection will throw ObjectDisposedException which the callback thread can catch, clean up any loose ends, and exit cleanly.

We realize this must be a common question, so someone on my team is planning to blog about this soon.

解决方案

Switch to the asynchronous version: BeginWaitForConnection.

If it does ever complete, you'll need a flag so the completion handler can just call EndWaitForConnection absorbing any exceptions and exiting (call End... to ensure any resources are able to be cleaned up).

这篇关于什么是一个好办法阻塞NamedPipeServer#为waitForConnection关机线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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