Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to run two methods from another class simultaneously. Obviously, the code below will not work. How can I do something like this?

C#
Node node = new Node();
EHNode ehnode = new EHNode();

Thread normalNode = new Thread(node.RunNode());
Thread ehNode = new Thread(ehnode.RunNode());

normalNode.Start();
ehNode.Start();
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-13 1:49am    
Event though this is formulated in somewhat naive way (but maybe very expressive), this is an interesting and important question. (I up-voted it with 4.) I have a good answer to it.
—SA

Yes, the question is interesting. My method of solving such problem is using a thread wrapper. The idea is: pass "this" to the thread, which is the reference to the wrapper. This will open all the instance members (not only instance methods) of the wrapper to the thread code. (Don't forget to use thread synchronization if you share any of them, but a thread wrapper also helps to encapsulate the synchronization primitives nicely.)

For further detail, please see my past answers, complete with code samples:
http://www.codeproject.com/Answers/155852/How-to-pass-ref-parameter-to-the-thread#answer2[^],
http://www.codeproject.com/Answers/223412/change-paramters-of-thread-producer-after-it-start#answer1[^],
http://www.codeproject.com/Answers/485734/MultiThreadingplusinplusC-23#answer4[^].

Did you get the idea?

—SA
 
Share this answer
 
Comments
Nico Encarnacion 13-Mar-13 13:05pm    
Hey Sergey, thanks for your answer. However, I received an answer from another forum, implemented it, and it worked just the way I wanted it to. Here's the simple solution:

Thread normalNode = new Thread(new ThreadStart(node.RunNode));
Thread ehNode = new Thread(new ThreadStart(ehnode.RunNode));

But can you further explain what the difference is between this solution and yours? :)

Thanks!!!
Sergey Alexandrovich Kryukov 13-Mar-13 13:34pm    
I already explain everything. You have access to all instance methods of the wrapper at once. Moreover, you can encapsulate thread synchronization inside wrapper, so every public or internal method will be synchronized between threads in a way invisible by the user but excluding a mistake.

My method is so beneficial that your simple alternative makes no sense. Passing anything else to ThreadStart has the following potential vulnerability: you have to down-cast the passed parameter.

I suggest you use my method, because you always have the chance that modify your code. The method you use contradicts the open-close principle and thus bad for support as more error-prone. Pretty obvious.
I also suggest you learn and understand my method and accept my answer formally (green button).
—SA
Do you have the sources for Node and EHNode, resp.? Then you could add a method StartRunNode() which creates a new thread with RunNode(), starts it, and returns to the caller.
 
Share this answer
 

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