Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've built one small multi threading windows application which is consist of single Win Form. Other than Main Thread, i'm displaying Message Box in new thread which i have created for prompt the permission to do the action. If user Select "Yes" then it'll continue. if "No" then it will close. It's working fine.

But i have problem in when Message Box appears, I shouldn't get capable to close Winform unless i close the Message Box. But in my case it's happening reversal. Even though Message Box Appeared i can able to close win From without closing Message Box which has raised from Created New Thread.
Please guide us. How make to solve this.
Posted
Updated 26-Dec-13 22:50pm
v3

1 solution

The problem is that the MessageBox is being created on a different thread to the main UI - so despite being Modal, it is't Modal for the UI thread - it only blocks execution on the new thread.

So you can use UI controls perfectly happily while the MessageBox is there - which is after all the point of using separate threads! The trouble is that if you close the main window, the thread code is still running...

Firstly. change the thread from a Thread class to a BackgroundWorker - that way at least if the main window is closed and the application exits, the thread will also be terminated - standard Thread class instances are independent and will continue to run even if the app shuts down. So if you do close your main form, at least the message box will be closed and the thread terminated as well.

If you really want the thread to "freeze" the main window until the message box has been acknowledged by the user, then you will have to have the thread as the UI to display the MessageBox and wait until the UI has had the user acknowledgement before continuing. This is possible, but is rather more involved than just calling MessageBox.Show!
 
Share this answer
 
Comments
mn.sathish 27-Dec-13 5:07am    
First i appreciate your quick reply....,
Thanks for your help and it's helped me lot....,
OriginalGriff 27-Dec-13 5:12am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900