Thread problem.
-
Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?
-
Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?
-
Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?
-
Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?
Hi! The whole
while(...
construct is superfluous. You start 3 threads and then callJoin()
for each of them. So the lastJoin()
will return only when NormalThread has finished and the other two threads have finished as well. Depending on how you access the thread's "results", declaring the respective members asvolatile
might help.Regards, mav -- Black holes are the places where God divided by 0...
-
Repost. Though I'd seen it before and yes, a week ago you posted the same code. A complete c+p actually. No-one helped you then, what makes you think they will now?
He who makes a beast out of himself gets rid of the pain of being a man
So what? He is trying again and may have better luck. Why didn't you just ignore his post?
-
Hi All, I need to run 3 methods parallaly.Then I have used Threading for that. Here is the code. public void threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); SpecialThread.Join(); BulkThread.Join(); NormalThread.Join(); while((NormalThread.ThreadState==System.Threading.ThreadState.Running) || (BulkThread.ThreadState==System.Threading.ThreadState.Running) || (SpecialThread.ThreadState==System.Threading.ThreadState.Running) ) { } } But most of time one or two threads are going to aborted or something happen and I loose their results.Sometimes this worked properly and I can get all results from 3 methods. Can any one correct this code?
I suggest using 3 AutoResetEvent that each thread would Set() when done. So, you start your three thread, and then, in your main thread, do a WaitHandle.WaitAll(....). It would be advised to use the overload with a timeout. Your code could look something like this:
public MyClass { private AutoResetEvent _specialEvent = new AutoResetEvent(); private AutoResetEvent _normalEvent = new AutoResetEvent(); private AutoResetEvent _bulkEvent = new AutoResetEvent(); public threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); WaitHandle.WaitAll(new WaitHandle[] { this._specialEvent, this._normalEvent, this._bulkEvent } ); // Do stuff with the result. } public GetNormal() { // Do normal stuff. this._normalEvent.Set(); } public GetSpecial() { // Do special stuff. this._specialEvent.Set(); } public GetBulk() { // Do bulkstuff. this._bulkEvent.Set(); } }
You could consider using the BackgroundWorker class also.
-
Hello, Why are you doing the "Join()"? What do the Methods of the Threads do? What are the results you are expecting and where do you get them from?
All the best, Martin
hi, thanks for reply. I am joining 3 threads with Join() method. Then 3 threads can execute parallary. There are 3 methods ,all are return same types of arrays.
-
I suggest using 3 AutoResetEvent that each thread would Set() when done. So, you start your three thread, and then, in your main thread, do a WaitHandle.WaitAll(....). It would be advised to use the overload with a timeout. Your code could look something like this:
public MyClass { private AutoResetEvent _specialEvent = new AutoResetEvent(); private AutoResetEvent _normalEvent = new AutoResetEvent(); private AutoResetEvent _bulkEvent = new AutoResetEvent(); public threadtest() { Thread SpecialThread = new Thread(new ThreadStart(this.GetSpecial)); Thread BulkThread = new Thread(new ThreadStart(this.GetBulk)); Thread NormalThread = new Thread(new ThreadStart(this.GetNormal)); SpecialThread.Start(); BulkThread.Start(); NormalThread.Start(); WaitHandle.WaitAll(new WaitHandle[] { this._specialEvent, this._normalEvent, this._bulkEvent } ); // Do stuff with the result. } public GetNormal() { // Do normal stuff. this._normalEvent.Set(); } public GetSpecial() { // Do special stuff. this._specialEvent.Set(); } public GetBulk() { // Do bulkstuff. this._bulkEvent.Set(); } }
You could consider using the BackgroundWorker class also.
Hi, thanks a lot for reply. I will check with your codes and comeback soon. Is there a problem ,when I am using Join() method?. I need to run my 3 methods paralaly. all the 3 methods return same type of arrays.