Vote count:
0
I try to understand communication between processes. Now I can send message from one thread to another:
class ProgramPipeTest
{
public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
Console.WriteLine("Connection lost");
}
public void ThreadStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
// Only continue after the server was created -- otherwise we just fail badly
// SyncClientServer.WaitOne();
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("mytestpipe"))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();
Console.WriteLine("[Client] Pipe connection established");
using (StreamWriter sw = new StreamWriter(pipeStream))
{
sw.AutoFlush = true;
string temp;
Console.WriteLine("Please type a message and press [Enter], or type 'quit' to exit the program");
while ((temp = Console.ReadLine()) != null)
{
if (temp == "quit") break;
sw.WriteLine(temp);
}
}
}
}
static void Main(string[] args)
{
// To simplify debugging we are going to create just one process, and have two tasks
// talk to each other. (Which is a bit like me sending an e-mail to my co-workers)
ProgramPipeTest Server = new ProgramPipeTest();
ProgramPipeTest Client = new ProgramPipeTest();
Thread ServerThread = new Thread(Server.ThreadStartServer);
Thread ClientThread = new Thread(Client.ThreadStartClient);
ServerThread.Start();
ClientThread.Start();
}
How can I send received message from client thread to another thread? I mean if there is something like dependency graph with three or more threads (1->2->3), so thread number 2 must be client, but after receiving message from server it has to send this message to thread number 3 (and now it is server). So, how can I implement this?
Thank you very much!
asked 6 mins ago
Aucun commentaire:
Enregistrer un commentaire