473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
May 31 '08 #1
7 1385
Pubs <pu******@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
May 31 '08 #2
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
May 31 '08 #3
On May 31, 11:40*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Pubs <pubud...@gmail.comwrote:
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.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Jon Skeet

Thank you very much for the help. It works now.
How do I know once the function is completed, thread is terminated ?

Pubudu
May 31 '08 #4
On May 31, 11:43*am, Marc Gravell <marc.grav...@gmail.comwrote:
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
May 31 '08 #5
On Sat, 31 May 2008 09:51:15 -0700, Pubs <pu******@gmail.comwrote:
Thank you very much for the help. It works now.
How do I know once the function is completed, thread is terminated ?
One approach would be to have your thread call a specific method when it's
done. If you want the method to do something with the UI, you should do
as Marc suggests with that method, calling it with Control.Invoke()
instead of calling it directly.

Pete
May 31 '08 #6
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
May 31 '08 #7
On May 31, 4:26*pm, Marc Gravell <marc.grav...@gmail.comwrote:
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
May 31 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
4
by: bughunter | last post by:
In version 7.x connect to db activate db db SQL1494W Activate database is successful, however, there is already a connection to the database. works fine - db activated. In 8.x - No!...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
14
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
7
by: Pubs | last post by:
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...
6
by: Eng Teng | last post by:
How do I write a code to activate a popup menu when I right click in a label control? Regards, Tee
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.