473,586 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best practice for waiting for worker thread to complete

Hello:

I would like to know the best way to spend idle cycles while waiting for a
thread to complete. I've seen numerous conversations on this group about how
unnatural Application.DoE vents() is, but can't find a good recommendation on
what I *should* do.

<Background>
I have inherited a C# state-machine application: i.e. Step 2 begins when
Step 1 is completed; Step 3 when Step 2 completes, etc. The code is
implemented within one controller function, rather than being event-based,
that looks something like:

Controller()
{
BeginInvoke(Pro ducerThread);

DisplayPageSync hronous(Page1);
int result = DisplayPageSync hronous(Page2);
DisplayPageSync hronous((result == 1 ? Page3 : Page4));
...
}

Page2 is a thumbnail view of the data the ProducerThread produces. In the
legacy codebase, Page2 blocked with a Monitor.Enter call; I would like to
add either progressive display of the thumbnails or a simple progress
indicator.

Since, unfortunately, Page2 is displayed synchronously, I need its message
pump to keep running while it waits for the producer to complete, which
really sounds like a job for a while() loop with a DoEvents/Sleep pair
inside it. But the wisdom of this group is to avoid such a call.

Thus, my question: What is the best practice for waiting for a thread to
finish? Am I missing something I could do with, e.g. the AutoResetEvent or
Monitor.Pulse?
Thank you for any and all help,
/m
Nov 17 '05 #1
3 13427
You can either set up an IAsync, which I think is painful and a lot of
work, or you can add a backgroundworke r control (2.0), and the
workercompleted even will fire when its done... (you can do whatever you
want in that time, including being idle)...

one page with a few references to the background worker control is here:
http://weblogs.asp.net/rosherove/arc...16/156948.aspx

this one is also popular:
http://www.mikedub.net/mikeDubSample...owsForms20.htm

or this is a fairly good video on IAsync by Mike Taulty:
http://www.microsoft.com/uk/asx/msdn...rvicecalls.asx

Goodluck...

MarkR wrote:
Hello:

I would like to know the best way to spend idle cycles while waiting for a
thread to complete. I've seen numerous conversations on this group about how
unnatural Application.DoE vents() is, but can't find a good recommendation on
what I *should* do.


Nov 17 '05 #2
MarkR <ma**@bogus.yah oo.com> wrote:

<snip>
Thus, my question: What is the best practice for waiting for a thread to
finish? Am I missing something I could do with, e.g. the AutoResetEvent or
Monitor.Pulse?


Your UI thread should just keep running the message pump, not in your
code. Disable all the controls which you want to be inaccessible while
the worker thread is running, and then make the worker thread call back
to the form when it's finished.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Thank you both for your replies. It appears you're both advocating switing
over to a more event-driven flow, which makes total sense, rather than a
kludgey wait loop.

I appreciate your help,
/m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
MarkR <ma**@bogus.yah oo.com> wrote:

<snip>
Thus, my question: What is the best practice for waiting for a thread to
finish? Am I missing something I could do with, e.g. the AutoResetEvent
or
Monitor.Pulse?


Your UI thread should just keep running the message pump, not in your
code. Disable all the controls which you want to be inaccessible while
the worker thread is running, and then make the worker thread call back
to the form when it's finished.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4

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

Similar topics

44
2347
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker thread. The worker thread is reading the serial port, waiting for something to happen (a service request). When it does it raises an event. Of course,...
39
789
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have completed. How can I do this? If I were to just put a Thread.Sleep with some arbitrary number ( say 5 minutes ) would the asynch web services...
11
9230
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
2
1302
by: Brian Henry | last post by:
I need to have an app that will have a queue, but sit dormant until something is placed in that queue, when it realizes the queue has items waiting start executeing the processes for thoes items... whats the best way to do this? I tried do loops and some attempts at threats for this... do loops uses process speed up cause of the constant...
5
3532
by: Soren S. Jorgensen | last post by:
Hi, In my app I've got a worker thread (background) doing some calculations based upon user input. A new worker thread might be invoked before the previous worker thread has ended, and I wan't only one worker thread running at any time (if a new worker thread start has been requested, any running worker thread results will be invalid). I'm...
3
1702
by: Michael D. Ober | last post by:
If I use standard Threading.Thread threads, I can issue a Join on the thread variable and wait for it to complete. How can I do this on the Background Worker Threads in a threadpool without looping. Basically, I will be queuing an unknown number of worker threads in the pool and I need to wait for them all to complete. Thanks, Mike Ober.
6
2857
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for example) or is doing some long calculations.
8
1687
by: Carl Heller | last post by:
If I'm creating a class to do some work that I want threaded out, where's the best location to call ThreadStart? Or does it depend on the nature of the work? a. Call it outside the class, giving it the starting method of the class? b. Have the class create the thread itself? ie: x = new WorkerClass(); ioThread = new Thread(new...
0
2452
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a StopReader() method that simply aborts the worker thread (is there a better way for the above situation?). The StopReader creates an...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.