By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Refreshing modeless dialogs?

P: n/a
DoB
What is the preferred way to refresh modeless dialogs?

I am using the modeless dialog that shows a progress of a sequential
process.
The dialog class has nonvisual properties containg the progress status data
and the dialog window has a timer and refreshes on every tick, but it does
not seem to work correctly.

Maybe I am doing it all wrong... what is a suggested way of programing this,
provided that I want to have one general dialog for many sequential
processes?

I imagine the usage should look something like this:

void SequentialProcess()
{
int Step = 0;
ProgressDialog pd = new ProgressDialog();
pd.Show();

some_loop {
...
Step++;
pd.ProgressValue = Step; // this line should not refresh anything
because it would be too costly
}

pd.Close();
}

And the ProgressDialog should refresh itself, on timer for instance...

Regards,
DoB.
Nov 20 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 20 Nov 2008 02:58:50 -0800, DoB <Do*@dob.comwrote:
What is the preferred way to refresh modeless dialogs?

I am using the modeless dialog that shows a progress of a sequential
process.
The dialog class has nonvisual properties containg the progress status
data
and the dialog window has a timer and refreshes on every tick, but it
does
not seem to work correctly.
Without an actual concise-but-complete code example that demonstrates what
you're currently doing and how it doesn't work, it's very difficult to
offer useful advice.

That said...if your code looks anything like what you've posted here, the
main problem is likely to be the fact that you are showing the dialog
using the same thread that you're using for your "sequential process".
That won't work, because the thread where you create the modeless dialog
needs to be free to process window messages, including those used to
handle user input and repainting of the dialog itself (as well as any
other forms you may have).

So the first thing you need to do is move your processing to a different
thread, and leave the main GUI thread available to handle GUI updates.

If I understand your post, you are also concerned with the question of
updating the status too often. But, you should focus on getting the
status dialog to update at all and work correctly first. Then you can
advance to making it work more efficiently. When you get to that point,
you could in fact use a timer as you suggest. Or you could simply
calculate the percentage progress, rounding to the nearest percentage
point, compare that to the previous computation and do the actual update
of the dialog when the rounded value changes. That would guarantee that
you refresh the dialog only 100 more times after it's created, no matter
how often your "sequential process" updates the status.

Note that .NET provides a ProgressBar class that already works fine. You
may want to consider using that for your progress status display. There
is also a BackgroundWorker class you can use for the actual execution of
your "sequential process" and which includes a ProgressChanged event that
can be used to relay progress updates back to the main GUI thread (which
will be essential once you've correctly moved your processing to a
different thread).

Pete
Nov 20 '08 #2

P: n/a
DoB
That said...if your code looks anything like what you've posted here, the
main problem is likely to be the fact that you are showing the dialog
using the same thread that you're using for your "sequential process".
Yes... this seems to be the reason.
Ideally, I would prefer to create a separate thread for the dialog box
rather than for the SequentialProcess, byt I am no sure how to do that.

Anyway, thanks for your suggestions, maybe I will redesign the whole thing.

DoB.

P.S.
Can BackgroundWorker update the controls directly?
I mean... a form is creating a background worker designed to load the tree
view which is a time consuming process... is it safe to update the contents
of the tree view directly from within DoWork?
Nov 21 '08 #3

P: n/a
On Thu, 20 Nov 2008 17:45:39 -0800, DoB <Do*@dob.comwrote:
>That said...if your code looks anything like what you've posted here,
the
main problem is likely to be the fact that you are showing the dialog
using the same thread that you're using for your "sequential process".

Yes... this seems to be the reason.
Ideally, I would prefer to create a separate thread for the dialog box
rather than for the SequentialProcess, byt I am no sure how to do that.
You can put the dialog box in a different thread, but assuming you have
any other UI, that doesn't actually fix the underlying problem. You still
need that main GUI thread to remain unblocked.

If you don't have any other UI, then it is possible to put the dialog box
on a different thread without causing other problems. But, there'd really
be no advantage to doing so and it would unnecessarily complicate the code
you have.
[...]
Can BackgroundWorker update the controls directly?
I mean... a form is creating a background worker designed to load the
tree
view which is a time consuming process... is it safe to update the
contents
of the tree view directly from within DoWork?
No. You have to call Control.Invoke() or Control.BeginInvoke().
BackgroundWorker does that step for you (essentially) when the
ProgressChanged and RunWorkerCompleted events are raised, but the code
executing in the DoWork event handler is in fact executing on the
background thread. As such, it cannot directly access Control members
(other than the "magic five" that are allowed, of course :) ).

Pete
Nov 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.