简体   繁体   中英

What is the easiest way to “run an instance in a separate Thread”?

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:

System.Threading.Tasks.Task.Factory.StartNew( ( ) => p1.Run )
    .ContinueWith( ( t ) => p1.Fnc );

Or you create a small helper method:

class Program
{
    private static Process p1 = new Process();
    static void Main()
    {
        Console.WriteLine("Main is running in thread {0}", Thread.CurrentThread.ManagedThreadId);

        var t1 = new Thread(Helper);
        t1.Start();
        Console.ReadLine();
    }

    private static Helper( )
    {
        p.Run();
        p.Fnc();
    }
}

As @PVitt says it's not reliable test of the programm you gonna to deliver. What you need is real separate processes.

Create an executable and run it with different command line parameters and/or after use any RPC available in .NET framework to make them "talk" with each other.

There is no mechanism to select a specific thread to run a method unless that thread is explicitly designed to support this. Essential ingredients for such a thread is a dispatch loop and a thread-safe queue to receive parcels of work. Otherwise well covered in the literature as the producer/consumer problem . What you are asking for is otherwise simple to implement:

public void Run()
{
    Fnc();
}
Action action = () => { p.Run(); p.Fnc(); };
var t1 = new Thread(action);
t1.Start();

If you use .Net 4, I'd suggest using Tasks (http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx), it's really easy to manage/cancel them.

To run what you need just write:

Task.Factory.StartNew(()=>{p.Run(); p.Fnc();});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM