views:

92

answers:

1

i have this code in C#:

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));

It is not working. How can I tell it to call the method I gave it? Secondly, I want t3 and t4 to be activated after t1 and t2 finish running. How can I do that? Third, I want t1 and t2 to not block (so that t2 wouldn't have to wait until t1 finishes). Is what I did correct?

+4  A: 

"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.

Jon Skeet
thanks! you solved me the last two questions, but not the first.it's not working from other reason, the problem is in the function i gave the thread in th ctor. it tells me that he can't convert from void to system.threading.threadstart...
Why are you passing a constructor to a thread? What are you trying to do? Create an object in a thread because construction takes a long time?
Dave
no, thid code is in the ctor, i pass regular funcgtions to the thread
@aharont: Editing...
Jon Skeet
delegate function will be the better option
moon
working!! thanks man!
@moon: Yes, will add an edit for that.
Jon Skeet