I ask similar question here I use this sample to implement namedpipes in win form, but now I need to run that in console application so there is a problem in handle events, in original code some events declared and fired in some threads as following:
public class PipeServer {
public delegate void MessageReceivedHandler(byte[] message)
public event MessageReceivedHandler MessageReceived;
void ListenForClients() {
//Some code
Thread readThread = new Thread(Read) { IsBackground = true };
}
void Read(object clientObj) {
//Some Code
if(MessageReceived != null)
MessageReceived(ms.ToArray());
}
}
So in win form we use it like this:
public partial class Form1 : Form {
private PipeServer pipeServer = new PipeServer();
public Form1(){
pipeServer.MessageReceived += pipeServer_MessageReceived;
}
void pipeServer_MessageReceived(byte[] message) {
Invoke(new PipeServer.MessageReceivedHandler(Do_pipeServer_MessageReceived),
new object[] { message });
}
public void Do_pipeServer_MessageReceived(byte[] message ) {
//Do Job
}
So when I use Console Application I can’t use Invoke, Does any one know what is the implementation for this in console apps?
Finally I found that for each event that fired in
PipeServer classI remove that event and replace with the handler(onevent)so the above example will be like the following:And added a constructor for
PipeServer classfor needed implementation, at the end just declare an instance of PipeServer. This approach maybe not the standard implementation but worked for me.