I am trying to simulate a distributed algorithm by putting each process (class) to a separate Thread, so they will act as a real isolated processes. The processes should be able to communicate between each other.
What I am trying to do can be demonstrated by this piece of code:
public class Process
{
public void Run()
{
Console.WriteLine("Run called from thread {0}", Thread.CurrentThread.ManagedThreadId);
}
public void Fnc()
{
Console.WriteLine("Fnc called from thread {0}", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
}
}
class Program
{
static void Main()
{
Console.WriteLine("Main is running in thread {0}", Thread.CurrentThread.ManagedThreadId);
Process p1 = new Process();
var t1 = new Thread(p1.Run);
t1.Start();
// This should call Fnc() in t1 Thread. It should also return immediatelly not waiting for method Fnc() to finish.
p1.Fnc();
Console.ReadLine();
}
}
I am getting this output:
Main is running in thread 9
Run called from thread 10
Fnc called from thread 9
I want to get something like this:
Main is running in thread 9
Run called from thread 10
Fnc called from thread 10
Is it possible to achieve this kind of functionality?
Thank you!
You can use the Thread Parallel Library:
Or you create a small helper method: