NamedPipeServerStream 读取器/写入器线程 C#
我试图在两个线程(读取器和写入器)之间共享 NamedPipeServerStream 实例。该程序可以工作,但是当我尝试创建 ServerProcess 对象的两个实例时,编写器线程开始挂起。我哪里错了?
任何建议将不胜感激。
生成方法如下:
public class ServerPrcoess
{
private static NamedPipeServerStream m_NamePipeHandle;
public static void StartPipe()
{
int i = 0;
Thread[] OPCServerReader = new Thread();
Thread[] OPCServerWriter = new Thread();
m_NamePipeHandle = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 254, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
m_NamePipeHandle.WaitForConnection();
OPCServerReader = new Thread(PipeReaderThread);
OPCServerReader.IsBackground = true;
OPCServerReader.Start(i);
Thread.Sleep(1000);
OPCServerWriter = new Thread(PipeWriterThread);
OPCServerWriter.IsBackground = true;
OPCServerWriter.Start();
}
}
读取器线程与下面类似,并使用管道句柄创建二进制读取器对象:-
private static void PipeReaderThread(object ThreadNo)
{
Console.WriteLine("Reader Thread Number: {0}", (int)ThreadNo);
try
{
StreamOPCMessageReader oMsgReader = new StreamOPCMessageReader(m_NamePipeHandle);
...
写入器线程使用相同的管道句柄执行等效操作。
private static void PipeWriterThread(object _ThreadNo)
{
int ThreadNo = Thread.CurrentThread.ManagedThreadId;
try
{
StreamOPCMessageWriter oMsgWriter = new StreamOPCMessageWriter(m_NamePipeHandle);
I’m attempting to share a NamedPipeServerStream instance between two threads, a reader and a writer. The program works but when I attempt to create two instances of the ServerProcess object the writer thread starts hanging. Where am I going wrong?
Any suggestions will be greatly appreciated.
The spawning method is as follows:
public class ServerPrcoess
{
private static NamedPipeServerStream m_NamePipeHandle;
public static void StartPipe()
{
int i = 0;
Thread[] OPCServerReader = new Thread();
Thread[] OPCServerWriter = new Thread();
m_NamePipeHandle = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 254, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
m_NamePipeHandle.WaitForConnection();
OPCServerReader = new Thread(PipeReaderThread);
OPCServerReader.IsBackground = true;
OPCServerReader.Start(i);
Thread.Sleep(1000);
OPCServerWriter = new Thread(PipeWriterThread);
OPCServerWriter.IsBackground = true;
OPCServerWriter.Start();
}
}
The reader thread is similar to below and creates a binary reader object using the pipe handle :-
private static void PipeReaderThread(object ThreadNo)
{
Console.WriteLine("Reader Thread Number: {0}", (int)ThreadNo);
try
{
StreamOPCMessageReader oMsgReader = new StreamOPCMessageReader(m_NamePipeHandle);
...
And the writer thread does the equivalent using the same pipe handle.
private static void PipeWriterThread(object _ThreadNo)
{
int ThreadNo = Thread.CurrentThread.ManagedThreadId;
try
{
StreamOPCMessageWriter oMsgWriter = new StreamOPCMessageWriter(m_NamePipeHandle);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法创建多个具有相同名称 (PIPENAME) 的 NamedPipeServerStream 实例。
You can not create multiple instances of NamedPipeServerStream with the same name(PIPENAME).