MessageBox.Show behaving Non-Modal

G

Guest

I have a windows form that contains a few windows controls such as buttons, labels and textboxes. In the Load event of the Form, I'm calling a HelloWorld webmethod asynchronously. The webmethod is a simple function that counts upto 5000, then returns a string. In the callback, I'm invoking a delegate that displays the string returned by the webmethod via MessageBox.Show

When I run the windows application, immediately switch to another application before the form to becomes visible, and switch back to the winsows form after the webmethod has finished running, the messagebox appears modally as it should. When I run the windows application, and wait for the webmthod to finish running, the messagebox appears modally as it should.

When I run the windows application and wait for the form to become visible, immediately switch to another application, and switch back to the winsows form after the webmethod has finished running, the messagebox appears modeless, which allows me to focus on controls and type/click in/on the controls. Any idea why this happens or how to avoid it

Thanks.
 
K

Kent Boogaart

You need to make sure that the code that displays the message box is
executing on the UI thread. Use the Control.Invoke() method.

HTH,
Kent

Norman Dotti said:
I have a windows form that contains a few windows controls such as
buttons, labels and textboxes. In the Load event of the Form, I'm calling a
HelloWorld webmethod asynchronously. The webmethod is a simple function that
counts upto 5000, then returns a string. In the callback, I'm invoking a
delegate that displays the string returned by the webmethod via
MessageBox.Show.
When I run the windows application, immediately switch to another
application before the form to becomes visible, and switch back to the
winsows form after the webmethod has finished running, the messagebox
appears modally as it should. When I run the windows application, and wait
for the webmthod to finish running, the messagebox appears modally as it
should.
When I run the windows application and wait for the form to become
visible, immediately switch to another application, and switch back to the
winsows form after the webmethod has finished running, the messagebox
appears modeless, which allows me to focus on controls and type/click in/on
the controls. Any idea why this happens or how to avoid it?
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?Tm9ybWFuIERvdHRp?= said:
I have a windows form that contains a few windows controls such as buttons, labels and textboxes. In the Load event of the Form, I'm calling a HelloWorld webmethod asynchronously. The webmethod is a simple function that counts upto 5000, then returns a string. In the callback, I'm invoking a delegate that displays the string returned by the webmethod via MessageBox.Show.

When I run the windows application, immediately switch to another application before the form to becomes visible, and switch back to the winsows form after the webmethod has finished running, the messagebox appears modally as it should. When I run the windows application, and wait for the webmthod to finish running, the messagebox appears modally as it should.

When I run the windows application and wait for the form to become visible, immediately switch to another application, and switch back to the winsows form after the webmethod has finished running, the messagebox appears modeless, which allows me to focus on controls and type/click in/on the controls. Any idea why this happens or how to avoid it?

Are you sure the messagebox is displayed in the app's (main) UI thread?
 

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