Click here to Skip to main content
15,112,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 threads created from the same ParameterizedThreadStart,
this delegate encapsulates a method with 1 argument of object,

I want to execute each thread with different arguments passed into the delegate.
But I don't know how to do, for example, the 3 arguments are a1, a2, a3.

The method is called MakeGrey, I want to execute 3 threads t1, t2, t3 with t1 executing MakeGrey(a1), t2 executing MakeGrey(a2), t3 executing MakeGrey(a3).

Could you tell me how to do it? I know we have to have an If statement at the beginning of the method MakeGrey to check if InvokeRequired to call Invoke, but calling this Invoke method requires a parameterizedthreadstart and some argument and I don't know which argument is passed into Invoke because I have 3 arguments a1, a2, a3 as mentioned above.

Hope you understand what I'm being stuck at and help me!
Your help would be highly appreciated!
Thank you!
Posted
Updated 3-Jan-12 23:47pm
v2
Comments
Nicholas Butler 4-Jan-12 7:28am
   
You can use a closure: new Thread( () => MakeGrey( ...pass your argument here... ) );
supernorb 4-Jan-12 20:34pm
   
Thank you! You gave me a very short way of creating new thread with arguments, I have already known how to do it another way longer but how it is done is not important, I want to know what we have to do if happenning a cross thread exception? The normal way I see others use for this is to check if InvokeRequire, we call Invoke with a delegate of MakeGrey and some arguments passed into, the problem shows up here is I have 3 arguments for 3 speparate threads (a1, a2, a3). Suppose if thread 1 requires invoke, I should know it to call Invoke(ParameterizedThreadStartOfMakeGrey, a1) only not all calls with all a1, a2, a3, shouldn't I? That's my problem. Or if you can tell me how to avoid cross-thread exceptions, please do it as a favour for me!
Thank you!

1 solution

   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900