"It is not working" isn't a very clear set of symptoms. What are you observing?
EDIT: Okay, now that you've said what the compiler error is, it's much easier to diagnose. You're currently calling a method and trying to use the result as a task for the thread to execute. Assuming you actually want to make that method call when the thread is started, you want something like this:
C# 2:
Thread t1 = new Thread(delegate() { functionsActivations(3, 4000, 0, 4); });
C# 3:
Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4));
An altnerative to having lambda expressions all over the place would be to write a utility method:
private static Action DeferFunctionActivations(int a, int b, int c, int d)
{
return () => functionsActivations(a, b, d, d);
}
Then you could use:
Thread t1 = new Thread(DeferFunctionActivations(3, 4000, 0, 4));
etc.
For the rest of the post I'll assume C# 3.
Additionally, t1.start()
should be t1.Start()
- C# is case sensitive.
To answer your final point, t1
and t2
are currently independent - they won't be blocking each other unless you've got synchronization somewhere in the code that they're running.
If you only want t3
and t4
to start when t1
and t2
have finished, you could use Thread.Join
:
Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4));
Thread t2 = new Thread(() => functionsActivations(3, 4000, 5, 9));
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Thread t3 = new Thread(() => functionsActivations(4, 4000, 0, 4));
Thread t4 = new Thread(() => functionsActivations(4, 4000, 5, 9));
t3.Start();
t4.Start();
Note that that means that this thread will wait until t1
and t2
have finished, too. If that's not good enough for you, there are various options but basically you'll want something else to asynchronously wait for t1 and t2 to complete. For example, you could tie up an extra thread to do that:
Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4));
Thread t2 = new Thread(() => functionsActivations(3, 4000, 5, 9));
t1.Start();
t2.Start();
Thread t3 = new Thread(() => functionsActivations(4, 4000, 0, 4));
Thread t4 = new Thread(() => functionsActivations(4, 4000, 5, 9));
Thread t5 = new Thread(() =>
{
t1.Join();
t2.Join();
t3.Start();
t4.Start();
});
t5.Start();
Somewhat icky, but it should work.
Are you able to use .NET 4.0? If so, the Parallel Extensions framework makes a lot of this significantly easier.