日期:2014-05-18 浏览次数:21080 次
while (true)
{
Console.WriteLine("请输入信息");
string msg = Console.ReadLine();
if (msg == "EXIT")
break;
byte[] msgBuffer = Encoding.UTF8.GetBytes(msg);
IntPtr fileHandle;
while (true)
{
// 创建命名管道
fileHandle = NamedPipeNative.CreateFile(pipeName,
NamedPipeNative.GENERIC_READ | NamedPipeNative.GENERIC_WRITE,
0, null, NamedPipeNative.OPEN_EXISTING, 0, 0);
// 创建失败时,跳出循环
if (fileHandle.ToInt32() != NamedPipeNative.INVALID_HANDLE_VALUE)
break;
// 无法打开管道时,不再执行下面的操作
if (NamedPipeNative.GetLastError() != NamedPipeNative.ERROR_PIPE_BUSY)
{
Console.WriteLine("不能打开管道");
return;
}
if (!NamedPipeNative.WaitNamedPipe(pipeName, 20000))
{
Console.WriteLine("不能打开管道");
return;
}
}