Threads question! Very important :-)

A

alex

HI.

I am new to threads, need an advice.
I have a form and need to run time consuming process on button click.
SO while in this process my form freses up and wouldnt move, etc. All
i could do just use form.refresh to see updated progress bar, but its
not enuf.

I was trying to put this "long" function on the second thread using
sample from MSDN, but it wouldnt help. Here is the sample:

Thread t = new Thread(new ThreadStart(ThreadJob));
t.Start();
Thread.Sleep(0);
t.Join();

private void ThreadJob()
{
//do something loooooooooooooooong
Thread.Sleep(0);
}

Form still frosen during ThreadJob(). WHat do I do wrong? How to
"thread out" long running processes from form user interface?? Please
help!
 
H

Herfried K. Wagner [MVP]

(e-mail address removed) (alex) scripsit:
I have a form and need to run time consuming process on button click.
SO while in this process my form freses up and wouldnt move, etc. All
i could do just use form.refresh to see updated progress bar, but its
not enuf.

I was trying to put this "long" function on the second thread using
sample from MSDN, but it wouldnt help. Here is the sample:

Thread t = new Thread(new ThreadStart(ThreadJob));
t.Start();
Thread.Sleep(0);
t.Join();

private void ThreadJob()
{
//do something loooooooooooooooong
Thread.Sleep(0);
}

Form still frosen during ThreadJob(). WHat do I do wrong? How to
"thread out" long running processes from form user interface??

Have a look [1].

Footnotes:
[1] <http://www.devx.com/dotnet/Article/11358>.
 
D

Doug Forster

Hi Alex,

You have explicitly blocked your main thread till ThreadJob is finished with
the t.Join()

Cheers

Doug Forster
 
1

100

Hi alex,
You block your main thread when you do *t.Join()*
This is the same as if you just use your main thread for doing the lengthy
operation.

Instead of joining the worker thread you should let the main thread go and
use some kind of noification and synchronization with the worker thread. For
notification purposes you can use events., Be careful if you are going to do
any kind of UI update you should consider using Control.Invoke to switch
execution over the main thread before touching any UI releated think. For
synchronization there is bunch of classes and techniques to use. With c# the
easiest are critical sections. For critical sections you can use the C#'s
*lock* operator. If you want to have more control on the critical sections
you can use Monitor class. Both (lock and Monitor) can be used together.
Actually usages of the *lock* operator are translated to Monitor's methods
calls. The last thing I want to warn you is the following. Even if you use a
background thread as a worker thread (this is by default and is more natural
for the worker thread to be a background thread) it is possible when the
user closes the application for the main thread to die before the worker
thread. So you have to notify the worker thread that it has to finish
earlier and keep the main thread alive until it receives notification from
the worker thread that it got the request. To kill the worker thread form
outside is not good practice and should be avoided.

HTH
B\rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top