Jeff
Jeff

Reputation: 36603

ShowDialog without Blocking Caller

I have a strong named assembly.

This has been asked before...but only sort of and with a different purpose.

I have a Form base class. When the implementing class sets a property on the base class IsBusy. I want to block all interaction with the Form (setting Enabled = false is not enough - I also want to block moving, resizing, closing, etc....and I don't want my controls to look disabled when IsBusy == true) and show a popup loading form in front (a transparent, borderless form with a loading animation).

Calling ShowDialog on my loading animation form does the trick in terms of blocking interaction on the calling form...but obviously I also want the calling forms code to continue executing.

Right now I'm using new LoadingForm().Show(), then handling WndProc on my calling Form and if IsBusy == true I supress all WndProc messages...but I don't like this approach. It prevents the form from repainting too, which I don't want.

I wouldn't mind the WndProc approach so much if I knew all the different types of messages to let through to allow correct repainting while IsBusy == true...but I don't.

So, my question is:

Is there a better solution?

or

Cans someone tell what WndProc messages I should let through? Or where to find a glossary?

Thanks.

Upvotes: 4

Views: 8222

Answers (3)

Jeff
Jeff

Reputation: 36603

I ended up BeginInvoke'ing a ShowDialog:

myForm.BeginInvoke(new Action(() => new LoadingForm().ShowDialog()));

that has the desired effect of letting code after that line continue to run and still blocking all interaction with myForm.

Upvotes: 14

Hans Passant
Hans Passant

Reputation: 942448

You are making it too complicated. All you have to do is prevent the dialog from getting closed. Implement the FormClosing event (or override OnFormClosing, better) and set e.Cancel = true while the operation is busy. There's then nothing the user can do to disturb your process.

This is all assuming that you used a worker thread to implement the operation.

Upvotes: 2

Blindy
Blindy

Reputation: 67544

If you want to let through painting, allow WM_PAINT and WM_NCPAINT.

Upvotes: 3

Related Questions