如何解锁ConnectNamedPipe和ReadFile? [C#]

发布于 2024-08-03 04:01:26 字数 1283 浏览 4 评论 0原文

我有一个类(NamedPipeManager),它有一个线程(PipeThread),它等待使用(ConnectNamedPipe)的NamedPipe连接,然后读取(ReadFile) - 这些是阻塞调用(不重叠) - 然而,当我想要时,有一个点取消阻止它们 - 例如,当调用类尝试停止 NamedPipeManager 时...

我怎样才能中断它?使用Thread.abort?线程.中断?有没有正确的方法来处理这个问题? 请参阅下面的代码,它说明了我当前的情况

main()
{
    NamedPipeManager np = new NamedPipeManager();
        ... do stuff ...
    ... do stuff ...
    np.Stop();      // at this point I want to stop waiting on a connection
}


class NamedPipeManager
{
private Thread PipeThread;

public NamedPipeManager
{
    PipeThread = new Thread(new ThreadStart(ManagePipes));
    PipeThread.IsBackground = true;
    PipeThread.Name = "NamedPipe Manager";
    PipeThread.Start();
}

private void ManagePipes()
{
    handle = CreateNamedPipe(..., PIPE_WAIT, ...);
    ConnectNamedPipe(handle, null);     // this is the BLOCKING call waiting for client connection

    ReadFile(....);             // this is the BLOCKING call to readfile after a connection has been established
    }


public void Stop()
{
    /// This is where I need to do my magic
    /// But somehow I need to stop PipeThread
    PipeThread.abort();     //?? my gut tells me this is bad
}
};

那么,在函数 Stop() 中 - 我如何优雅地解除对 ConnectNamedPipe(...) 或 ReadFile(...) 的调用?

任何帮助将不胜感激。 谢谢,

I have a class (NamedPipeManager) which has a thread (PipeThread) that waits for a NamedPipe connection using (ConnectNamedPipe) and then reads (ReadFile) - these are blocking calls (not-overlapped) - however there comes a point when I want to unblock them - for example when the calling class tries to stop the NamedPipeManager...

How can I interupt it? Using Thread.abort? Thread.interrupt? Is there a proper way to handle this?
Refer to the code below which illustrates my current situation

main()
{
    NamedPipeManager np = new NamedPipeManager();
        ... do stuff ...
    ... do stuff ...
    np.Stop();      // at this point I want to stop waiting on a connection
}


class NamedPipeManager
{
private Thread PipeThread;

public NamedPipeManager
{
    PipeThread = new Thread(new ThreadStart(ManagePipes));
    PipeThread.IsBackground = true;
    PipeThread.Name = "NamedPipe Manager";
    PipeThread.Start();
}

private void ManagePipes()
{
    handle = CreateNamedPipe(..., PIPE_WAIT, ...);
    ConnectNamedPipe(handle, null);     // this is the BLOCKING call waiting for client connection

    ReadFile(....);             // this is the BLOCKING call to readfile after a connection has been established
    }


public void Stop()
{
    /// This is where I need to do my magic
    /// But somehow I need to stop PipeThread
    PipeThread.abort();     //?? my gut tells me this is bad
}
};

So, in function Stop() - how would I gracefully unblock the call to ConnectNamedPipe(...) or ReadFile(...)?

Any help would be appreciated.
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掀纱窥君容 2024-08-10 04:01:26

如果我尝试通过以下方式中断 ConnectNamedPipe ,它似乎可以在 VC6.0、WinXP 上运行
DeleteFile("\\\\.\\pipe\\yourpipehere");

所以只需指定名称,而不是句柄。

It seems to be working on VC6.0, WinXP if I try to interrupt ConnectNamedPipe by
DeleteFile("\\\\.\\pipe\\yourpipehere");

So just specify name, not handle.

合久必婚 2024-08-10 04:01:26

从 Windows Vista 开始,有一个 CancelSynchronousIO 操作可用于线程。我认为没有 C# 包装器,因此您需要使用 PInvoke 来调用它。

在 Vista 之前,确实没有办法优雅地执行此类操作。我建议不要使用线程取消(这可能有效,但不够优雅)。最好的方法是使用重叠 IO。

Starting with Windows Vista, there is a CancelSynchronousIO operation available for threads. I don't think there is a C# wrapper for it, so you would need to use PInvoke to call it.

Before Vista, there isn't really a way to perform such an operation gracefully. I would advise against using thread cancellation (which might work, but doesn't qualify as graceful). Your best approach is to use overlapped IO.

ぃ双果 2024-08-10 04:01:26

最近我遇到一种情况,我无法使用异步重叠IO。我被困在 ConnectNamedPipe 中的服务器端。为了解锁线程并释放资源,我必须瞬间连接到与客户端相同的管道。

  1. 主线程接收停止信号
  2. 主线程为侦听线程设置停止事件
  3. 主线程连接到管道
  4. 如果成功(总是) - 立即关闭新创建的句柄

  1. 侦听器线程解锁
  2. 侦听器线程执行所需的任何操作

这对我来说非常有效。

要取消阻止ReadFile,需要连接并写入管道。预期效果相同。

Recently I was in a situation, I could not use the Async Overlapped IO. I was stuck on the server side within ConnectNamedPipe. To unlock the thread and free resources, I had to connect to the same pipe as a client for a split second.

  1. Main thread receives the stop signal
  2. Main thread sets the stop event for the listening thread
  3. Main thread connects to the pipe
  4. If succeeded (always) - closes the newly created handle immediately

  1. Listener thread unlocks
  2. Listener thread does whatever required

This worked for me very well.

To unblock ReadFile one needs to connect and write to the pipe. Same effect epected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文