简体   繁体   English

在.NET命名管道中指定默认等待超时

[英]Specifying default wait timeout in .NET named pipes

I am working with NamedPipeServerStreams in C# where our clients will be using the Win32 CreateFile and WaitNamedPipe methods to establish connections. 我正在使用C#中的NamedPipeServerStreams,我们的客户端将使用Win32 CreateFile和WaitNamedPipe方法建立连接。

Previously our server was also Win32 and was specifying the 'DefaultTimeOut' as part of the construction of the pipe when calling CreateNamedPipe(). 以前,我们的服务器也是Win32,并且在调用CreateNamedPipe()时将“ DefaultTimeOut”指定为管道构建的一部分。

From what I have read/seen, my understanding is that the clients then inherit this timeout value and use it when making calls to WaitNamedPipe. 从我所读/看的内容来看,我的理解是客户端随后将继承此超时值,并在调用WaitNamedPipe时使用它。

Microsoft's MSDN documentation for CreateNamedPipe has the following description for the DefaultTimeOut parameter - Microsoft的CreateNamedPipe的MSDN文档对DefaultTimeOut参数具有以下说明-

*nDefaultTimeOut [in] The default time-out value, in milliseconds, if the WaitNamedPipe function specifies NMPWAIT_USE_DEFAULT_WAIT. * nDefaultTimeOut [输入]如果WaitNamedPipe函数指定NMPWAIT_USE_DEFAULT_WAIT,则默认超时值(以毫秒为单位)。 Each instance of a named pipe must specify the same value.* 命名管道的每个实例必须指定相同的值。*

When changing to use the .NET NamedPipeServerStream there seems to be no way of passing this default timeout value into the constructor (or otherwise) and my clients are now regularly timing out on the WaitNamedPipe semaphore, where they were not previously. 更改为使用.NET NamedPipeServerStream时,似乎无法将此默认超时值传递给构造函数(或其他方式),并且我的客户端现在定期在WaitNamedPipe信号量上超时,而以前没有。 Without changing all our clients to specify their own timeouts which we'd like to avoid, I can't seem to find a way of doing this in .NET. 如果不更改所有客户端以指定自己希望避免的超时,则似乎无法在.NET中找到实现此目的的方法。 Surely the .NET NamedPipe code is just wrapping the Win32 API underneath? 当然,.NET NamedPipe代码只是将Win32 API包装在下面?

If all else fails you could always call CreateNamedPipe via P/Invoke, specifying the default timeout you want just as you did in your Win32 server, and then use the NamedPipeServerStream ctor overload which takes a SafePipeHandle , to wrap it. 如果所有其他方法均失败,则可以始终通过P / Invoke调用CreateNamedPipe ,指定所需的默认超时,就像在Win32服务器中所做的那样,然后使用NamedPipeServerStream ctor重载,这需要使用SafePipeHandle进行包装。

The rest of your .NET server code wouldn't need to change. 您的.NET服务器代码的其余部分无需更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM