|
A Question About Multi-Thread -- ClaudeYih --
Hi, everyone. I have a question about multi-thread in C#. In my program, I want to create some child threads to do some task and meanwhile to hold the parent thread until all the child threads terminate. What should I do? Is the following code alright? ... ... //Create an array of threads in above int j = 0; for (; j < 256; j++) { threadArr = new Thread(new ThreadStart(MultiInsert5000)); threadArr .Start(); } Thread.CurrentThread.Join(); // The following code is what I want to do after all the child threads terminate ... ... Please help me on this question. Your kindness will be appreciated. |
|
-- Gordowey --
Hi Claude, take a look at this example.. http://www.codeproject.com/vb/net/ThreadDeath.asp regards Alberto Claude Yih wrote: Hi, everyone. I have a question about multi-thread in C#. In my program, I want to create some child threads to do some task and meanwhile to hold the parent thread until all the child threads terminate. What should I do? Is the following code alright? ... ... //Create an array of threads in above int j = 0; for (; j < 256; j++) { threadArr = new Thread(new ThreadStart(MultiInsert5000)); threadArr .Start(); } Thread.CurrentThread.Join(); // The following code is what I want to do after all the child threads terminate ... ... Please help me on this question. Your kindness will be appreciated. |
|
-- Tito --
This might not be the best way to go about it, but you could loop through and test the Thread.IsAlive property. Something along the lines of bool ThreadAlive = true; while(ThreadAlive) { ThreadAlive = false; for(int i = 0; i < 256; i++) { ThreadAlive = ThreadAlive || threadArr .IsAlive(); } Thread.Sleep(0); //Placed in to free up the processor } //will reach here when all the threads are dead Once again, this is a very simple way and there might be a better way. |