Hi All,

I've been look for the solution for this problem for days.. any help is very much appreciated.

My project invovles a small task that requires to generate three concurrent threads, it looks like:

Main()

{

........ 

//Start three threads;

Start Thread1;
Start Thread2;
Start Thread3; 

//Wait for three threads to finish

Thread1.Join();
Thread2.Join();
Thread3.Join() 

Messagebox.Show("All threads finished");
......

}

Private void Thread1/2/3 ()

{
//some work here;
}

The code above works.. the only problem is that when the Main was waiting for three threads to finish, all components (eg.buttons) on the Main form were not responding..the Main form was locked...

I am wondering is there anyway to solve this problem?

I tried to use Thread.Sleep along with a While loop to wait:.

Main()

{
........ 

//Start three threads;

Start Thread1;
Start Thread2;
Start Thread3;

threadFinished = 0; 

//Wait for three threads to finish

While (threadFinished <3)

{
Thread.Sleep (5000);
}

Messagebox.Show("All threads finished");
......

} 

private void Thread1/2/3 ()

{
//some work here;

lock (this)
{
threadFinished++;
}

}

But it didn't work either..

I want that components on the main form can still work while it's waiting for other threads to finish.

Thanks in advance

Recommended Answers

All 2 Replies

You should separate the main thread from other threads as the main thread control GUI (e.g. buttons, menus,...) I didn't look at your code as I am sure I won't understand what you're willing to do... but whatever you create threads and they waiting each other and those threads far away the main thread, you won't get non reponse to your application

I have to agree here. I think you probably want to set those other threads to execute asynchronously after the form has been instantiated.

I'm not that up on threading, but I know that in order to make a form still responsive, you have to look at running the method in an asynchronous delegate, and then have another delegate get called with a callback method to get the result, IIRC... but I'm a bit rusty on that...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.