问题描述
大家好,
在写入NamedPipeServer时,这是一个阻塞调用,因此直到完全读取发生,它才不会被阻塞.
When writing on to a NamedPipeServer, it's a blocking call and so till a complete read happen's it doesn't get unblocked.
我们尝试从流中读取部分数据,并尝试处置/关闭NamedPipeServer.这导致了OperationCanceledException,该操作未在MSDN中进行记录.
We tried to read a part of data from the stream and tried disposing/closing the NamedPipeServer. This has resulted in OperationCanceledException which is not documented in MSDN for this operation.
实际上,它应该引发IO异常.
Effectively it should have raised IO Exception.
有人可以帮我解决这个问题.
Can someone help me out with this issue.
源代码::
字符串管道名称= Guid.NewGuid().ToString("N");
bool clientConnected = false;
bool writeFailed = false;
object syncBlock = new object();
使用(var server = new NamedPipeServerStream(pipename,PipeDirection.InOut,1,PipeTransmissionMode.Byte,PipeOptions.Asynchronous))
{
AsyncCallback waitCallback = ar =>
{
锁(syncBlock)
{
clientConnected =服务器!= null;
Monitor.PulseAll(syncBlock);
}
};
动作阻塞写入=()=>
{
var largeBuffer = new byte [1024 * 1024 * 128];
试试
{
锁(syncBlock)
{
server.Write(largeBuffer,0,largeBuffer.Length);
}
}
捕获(OperationCanceledException)
{
//预期
锁(syncBlock)
{
writeFailed = true;
Monitor.PulseAll(syncBlock);
}
}
};
var waitResult = server.BeginWaitForConnection(waitCallback,null);
使用(var client = new NamedPipeClientStream(.",管道名称,PipeDirection.InOut))
{
client.Connect();
锁(syncBlock)
{
while(!clientConnected)
{
Monitor.Wait(syncBlock);
}
}
server.EndWaitForConnection(waitResult);
var result = blockingWrite.BeginInvoke(null,null);
client.ReadByte();
server.Dispose();
锁(syncBlock)
{
while(!writeFailed)
{
Monitor.Wait(syncBlock);
}
}
BlockingWrite.EndInvoke(result);
}
}
string pipename = Guid.NewGuid().ToString("N");
bool clientConnected = false;
bool writeFailed = false;
object syncBlock = new object();
using (var server = new NamedPipeServerStream(pipename, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
AsyncCallback waitCallback = ar =>
{
lock (syncBlock)
{
clientConnected = server != null;
Monitor.PulseAll(syncBlock);
}
};
Action blockingWrite = () =>
{
var largeBuffer = new byte[1024 * 1024 * 128];
try
{
lock (syncBlock)
{
server.Write(largeBuffer, 0, largeBuffer.Length);
}
}
catch (OperationCanceledException)
{
// Expected
lock (syncBlock)
{
writeFailed = true;
Monitor.PulseAll(syncBlock);
}
}
};
var waitResult = server.BeginWaitForConnection(waitCallback, null);
using (var client = new NamedPipeClientStream(".", pipename, PipeDirection.InOut))
{
client.Connect();
lock (syncBlock)
{
while (!clientConnected)
{
Monitor.Wait(syncBlock);
}
}
server.EndWaitForConnection(waitResult);
var result = blockingWrite.BeginInvoke(null, null);
client.ReadByte();
server.Dispose();
lock (syncBlock)
{
while (!writeFailed)
{
Monitor.Wait(syncBlock);
}
}
blockingWrite.EndInvoke(result);
}
}
这样,阻塞的写操作不会从主线程中取消,而是放置了用于交换数据的管道.还可以通过同步确保线程在捕获异常之前一直处于活动状态.
As such the blocked write operation is not cancelled from the main thread, but the pipe used for exchanging the data was disposed. Also ensured via synchronization that the thread is alive till it catches the exception.
这主要是在.Net Framework 3.5上经历的
This was experienced primarily on .Net Framework 3.5
推荐答案
>>这导致了OperationCanceledException,此操作未在MSDN中进行记录.
>>This has resulted in OperationCanceledException which is not documented in MSDN for this operation.
当线程在取消线程正在执行的操作时抛出此错误.请检查是否有这样的线程.线程为这种情况提供了解决方案.
This error is thrown when a thread upon cancellation of an operation that the thread was executing. Please check if there is such thread. Thisthread supplies a solution for this scenario.
如果它不适合您,请提供信息作为项目代码(如果很大,您可以将其上传到SkyDrive并与我们共享链接),执行环境.
If it does not work for you, please provide information as the project code(if it is big, you could upload it SkyDrive and share the link with us), the executed environment.
此致.
这篇关于从NamedPipeServerStream的写入操作观察到未记录的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!