Modal dialogs

R

Reg Rat

Hi,

I'm trying to do something that appears extremely simple but I can't find an
obvious solution for it. I want to create a modal dialog which, upon being
displayed, immediately starts doing some work which may take some time (the
dialog is simply to display progress via a progress bar and an updating
label with a cancel button). The obvious solution is to put the call to the
method doing the work in the Load event handler of the dialog, but this
causes the work to start before the dialog has been displayed.

It looks like the only option I have is to kick off a new thread in the Load
handler which will do the work, but this seems a bit over the top for my
purposes, particularly as this thread would need to call into the UI thread
to update the dialog.

Is there an obvious answer I have missed?

Thanks in advance,
Mike
 
R

Richard Grimes

Reg said:
progress bar and an updating label with a cancel button). The obvious
solution is to put the call to the method doing the work in the Load
event handler of the dialog, but this causes the work to start before
the dialog has been displayed.

There are various events you can try: VisibleChanged, HandleCreated,
Activated, I cannot remember the order they are called, but they are
worth a try. Another option is the Paint event is called. If you use
..NET 2.0 you can use the Shown event.

Richard
 
P

Pascal Duti

Hi,

I come from a Delphi background so this might or might not work in .NET.

The idea is that in the Activated event of the form, you send a windows
message to the form itself. In the form you define an event handler for
that message and in that eventhandler you do the processing.

How does this resolve your problem ? The message you sent gets only
dispatched to the the form when all previous messages are processed.
Painting the form is also done via messages (WM_PAINT). So once you
receive your message the form is already painted and created (because
all WM_PAINT are processed before your message.

Sounds complicated but is really simple to do. If you know how to send a
message (PostMessage in Win API) and how to define an eventhandler for
messages (Dispatch ?)
 
A

Alan Pretre

Reg Rat said:
It looks like the only option I have is to kick off a new thread in the
Load handler which will do the work, but this seems a bit over the top for
my purposes, particularly as this thread would need to call into the UI
thread to update the dialog.

Hi. Use the BackgroundWorker class. You can set this all up in your
FormLoad event. It's the cat's meow.

http://msdn2.microsoft.com/en-us/library/4852et58.aspx

-- Alan
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top