How to use threading to activate a sperate function without freezingthe current window

  • Thread starter Thread starter Pubs
  • Start date Start date
P

Pubs

I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.

THis is what I did,

Thread FirstThread = new Thread(new ThreadStart(ThreadFunc));
FirstThread.Start();
FirstThread.Join();

protected static void ThreadFunc()
{
for (Int64 i = 0; i < 10000000; i++)
{
// Do my job . . . . .
}
}

Can some one help me to resolve this problem. I am still a bigginner.

Pubudu
 
Pubs said:
I am learning C sharp. I want to call a function (any simple function.
E.g. : Never ending while loop or large For loop inside the function).
I want to call this function from the main application window. I found
couple of tutorials but for some reason window freezes.

THis is what I did,

Thread FirstThread = new Thread(new ThreadStart(ThreadFunc));
FirstThread.Start();
FirstThread.Join();

When you call Join, that basically says "wait until the thread has
finished" - which completely negates the point of doing threading.

Just don't call Join and it should be fine.
 
You are calling Join, which by definition waits for the other thread
to exit. In short, don't call Join here - just start the other thread
and let it run. By the way, if the worker thread is touching the form
or any controls (such as setting text etc), you will need to use
Control.Invoke to switch back to the UI thread, due to thread
affinity.

Marc
 
You are calling Join, which by definition waits for the other thread
to exit. In short, don't call Join here - just start the other thread
and let it run. By the way, if the worker thread is touching the form
or any controls (such as setting text etc), you will need to use
Control.Invoke to switch back to the UI thread, due to thread
affinity.

Marc

Marc,

Thank you very much Marc. It is working fine.

Pubudu
 
To second Peter's answer: if the code in question is "close" to the
other code, then just call some method directly, usually via
Control.Invoke - i.e.

void WorkerMethod()
{ // runs on background thread
// ... some long code
this.Invoke((MethodInvoker) LongOpFinished);
}
void LongOpFinished()
{ // runs on UI thread when WorkerMethod is complete
}

If the code is more separated (such as library methods), then you
might use a "callback" - which at the simplest could be as simple as
passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
the method as an argument, and then invoking it at the end. Callbacks
can also be implemented via events (and about 20 other ways) - or you
can just have the UI worry about it locally (i.e. the UI calls a local
method 9worker thread) that calls the library method (worker thread),
then calls another local method (UI thread).

Marc
 
To second Peter's answer: if the code in question is "close" to the
other code, then just call some method directly, usually via
Control.Invoke - i.e.

void WorkerMethod()
{ // runs on background thread
  // ... some long code
  this.Invoke((MethodInvoker) LongOpFinished);}

void LongOpFinished()
{ // runs on UI thread when WorkerMethod is complete

}

If the code is more separated (such as library methods), then you
might use a "callback" - which at the simplest could be as simple as
passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
the method as an argument, and then invoking it at the end. Callbacks
can also be implemented via events (and about 20 other ways) - or you
can just have the UI worry about it locally (i.e. the UI calls a local
method 9worker thread) that calls the library method (worker thread),
then calls another local method (UI thread).

Marc

Marc,

Thank you very much. This helped me a lot.

Pubudu
 

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

Back
Top