C#所有管道实例都忙 [英] C# all pipe instances are busy
问题描述
以下代码创建一个新线程,该线程首先充当用于发送参数的命名管道客户端,然后充当用于检索结果的服务器.之后,它在另一个名为命名管道服务器的AppDomain中执行一个功能,然后作为客户端将结果发送回去.
The following code creates a new thread acting first as a named pipe client for sending parameters and then as a server for retrieving results. After that it executes a function in another AppDomain acting as a named pipe server and after that as a client to send the results back.
public OrderPrice DoAction()
{
Task<OrderPrice> t = Task<OrderPrice>.Factory.StartNew(NamedPipeClient, parameters);
if (domain == null)
{
domain = AppDomain.CreateDomain(DOMAINNAME);
}
domain.DoCallBack(AppDomainCallback);
return t.Result;
}
static OrderPrice NamedPipeClient(object parameters) {
OrderPrice price = null;
using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_TO)) {
stream.Connect();
SerializeToStream(stream, parameters);
}
using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_BACK)) {
stream.WaitForConnection();
price = (OrderPrice)DeserializeFromStream(stream);
}
return price;
}
void AppDomainCallback() {
OrderPrice price = null;
using (NamedPipeServerStream stream = new NamedPipeServerStream(PIPE_TO)) {
stream.WaitForConnection();
List<object> parameters = (List<object>)DeserializeFromStream(stream);
if (mi != null)
price = (OrderPrice)mi.Invoke(action, parameters.ToArray());
}
using (NamedPipeClientStream stream = new NamedPipeClientStream(PIPE_BACK)) {
stream.Connect();
SerializeToStream(stream, price);
}
}
该代码平均每秒被调用一次,并且可以正常工作7个多小时.但是有时会抛出"system.io.ioexception所有管道实例都处于繁忙状态",之后它们将不再重新连接.在这里浏览似乎是因为未正确放置管道对象,但我想这一切都很好,因为它们在using语句内部. 有谁知道这里可能有什么问题吗?该代码在Windows Server 2008上运行的.NET 4.0中.
The code is called once per second on average and it worked fine for 7+ hours. But at some point "system.io.ioexception all pipe instances are busy" is thrown and they wont reconnect anymore after that. Browsing here it seems like it could be because of not properly disposing the pipe objects, but I guess thats all good since they are inside using statements. Does anyone have any clue what could be wrong here? The code is in .NET 4.0 running on windows server 2008.
推荐答案
听起来应该是mutex
而不是简单的锁
Sounds like it should be a mutex
instead of a simple lock
就偶尔停止而言,可能是饥饿或僵局.
as far as the occasional halting, it could be starvation or a deadlock.
这是一本很好的阅读材料,摘要了可能发生的事情
This is good reading material for abstracts on what may be happening
http://en.wikipedia.org/wiki/Dining_philosophers_problem
这篇关于C#所有管道实例都忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!