A Question About Multi-Thread

1 view
Skip to first unread message

Claude Yih

unread,
Jul 18, 2006, 3:25:37 AM7/18/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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[j] = new Thread(new ThreadStart(MultiInsert5000));
threadArr[j].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

unread,
Jul 18, 2006, 4:36:36 PM7/18/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Claude, take a look at this example..

http://www.codeproject.com/vb/net/ThreadDeath.asp

regards

Alberto

Tito

unread,
Jul 19, 2006, 10:27:50 PM7/19/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
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[i].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.

Claude Yih

unread,
Jul 28, 2006, 12:13:44 AM7/28/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Tito wrote:

> bool ThreadAlive = true;
> while(ThreadAlive)
> {
> ThreadAlive = false;
> for(int i = 0; i < 256; i++)
> {
> ThreadAlive = ThreadAlive || threadArr[i].IsAlive();
> }
> Thread.Sleep(0); //Placed in to free up the processor
> }
>
> //will reach here when all the threads are dead
>

based on the above code, I wrote a function like this:

void WaitForThreadsEnd(Thread [] arr)
{
bool bChildThAlive = true;
int index;
while(true)
{
for (index = 0; index < arr.Length; index++)
{
bChildThAlive = arr[index].IsAlive;
if (bChildThAlive)
{
// If there is one child thread still alive,
// make the parent thread sleep for 1 sec
Thread.Sleep(1000);
break;
}
}
if ((!bChildThAlive) && (index == arr.Length))
{
break;
}
}
}

> Once again, this is a very simple way and there might be a better way.

I also need a simple way :)

Tito

unread,
Jul 29, 2006, 10:57:15 PM7/29/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
There is no need to wait a whole second on your sleep command...this
will slow your process way down. As long as you have at least a
Sleep(0), the processor will allow other command to step in and it will
not drag your cpu down. Also, to keep away from the breaks, you can
try something like the following:

void WaitForThreadsEnd(Thread [] arr)
{
bool bChildThAlive = true;

while(bChildThAlive)
{
bChildThAlive = false;


for (index = 0; index < arr.Length; index++)
{
bChildThAlive = arr[index].IsAlive ||

bChildThAlive;
}
//this for loop will set the var bChildThAlive = true
if there is at least on thread alive
Thread.Sleep(0);
}

}

Reply all
Reply to author
Forward
0 new messages