how would you do it?

J

John Davison

Given a piece of code that may take some time to complete, I want to
display an animation in a popup window. The popup window contains a
button allowing the user to cancel the operation. When the code starts,
the popup window shows, and when either the code is finished or the user
hits cancel, the window is closed.

The issue is threading. ShowDialog() won't work because you have to
execute the rest of the code. So we have to use Show(). However, the
control stops painting itself because the execution is doing something else.

The obvious solution is to create the window on another thread and let
it run over there. Then you get into all the issues of threading,
(BeginInvoke, Invoke, etc...) For example, one of the properties of the
window is Text, which changes the Text property of a Label. You can't
do that until the window is actually loaded. So my solution for that
would either be a) wait in the code for the form to be loaded, or b)
queue up changes to the Text property internally in the popup window
until the form is loaded.

It seems clunky to me to have to write another function just to load the
popup window (for the ThreadStart delegate.)

It always seems like I'm coming up with workarounds and hacks to do
stuff like this. Does anyone have any good techniques they would care
to share?

John Davison
 
C

Cordell Lawrence

Hey John,
It seems that if you want to run some processor intensive / time consuming
work while still having your application UI be responsive you can't really
get away from using threads. One thing to think about is what you use the
worker thread for. you said:

What I would do is create the dialog form on the Main thread but run the
time consuming process on another. The worker thread can safely cause
updates to the dialog form by using the Control.BeginInvoke() method of the
various controls on the dialog form and the form itself.

Hope this helps.
Cordell Lawrence
Teleios Systems Ltd.
 

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