private void simpleButtonStartserver_Click(object sender, EventArgs e)
{
this.textEdit1.Text = null;
NamedPipeServerThread = new Thread[maxNumberOfServerInstances];
#region 通过命名管道(NamedPipe)方式与winform通信
if (true)
{
for (int i = 0; i < NamedPipeServerThread.Count(); i++)
{
Thread foriNamedPipeServerThread = NamedPipeServerThread[i];
if (foriNamedPipeServerThread != null && foriNamedPipeServerThread.ThreadState != System.Threading.ThreadState.Aborted)
{
foriNamedPipeServerThread.Abort();
}
foriNamedPipeServerThread = new Thread(new ParameterizedThreadStart(ServiceNamedPipeServerThread))
{
IsBackground = true,
};
foriNamedPipeServerThread.Start(i);
}
}
#endregion
}
private void ServiceNamedPipeServerThread(object paraObject)
{
try
{
NamedPipeServerStream serverNamedPipeServerStream = null;
//创建实例
if (serverNamedPipeServerStream != null)
{
serverNamedPipeServerStream.Disconnect();
serverNamedPipeServerStream.Close();
serverNamedPipeServerStream.Dispose();
serverNamedPipeServerStream = null;
}
if (serverNamedPipeServerStream == null)
{
//PipeSecurity newPipeSecurity = new PipeSecurity();
//newPipeSecurity.SetAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
//serverNamedPipeServerStream = new NamedPipeServerStream(pipeNameNamedPipe, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 1024, 1024, newPipeSecurity);
serverNamedPipeServerStream = new NamedPipeServerStream(pipeNameNamedPipe, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 1024, 1024);
int threadId = Thread.CurrentThread.ManagedThreadId;
serverNamedPipeServerStream.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallback), serverNamedPipeServerStream);
}
}
catch (Exception catchException)
{
throw catchException;
}
finally
{
}
}
private void WaitForConnectionCallback(IAsyncResult callbackIAsyncResult)
{
try
&nbs