473,417 Members | 1,459 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,417 software developers and data experts.

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.DoEvents() 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(ProducerThread);

DisplayPageSynchronous(Page1);
int result = DisplayPageSynchronous(Page2);
DisplayPageSynchronous((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 13414
You can either set up an IAsync, which I think is painful and a lot of
work, or you can add a backgroundworker 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.DoEvents() is, but can't find a good recommendation on
what I *should* do.


Nov 17 '05 #2
MarkR <ma**@bogus.yahoo.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
MarkR <ma**@bogus.yahoo.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.com>
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
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...
39
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...
11
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...
2
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......
5
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...
3
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...
6
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...
8
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,...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.