470,626 Members | 2,116 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

Forcing MessageBox.Show to close from another thread

Fr33dan
57
Hi All,

I'm working on adding a message box method that will allow me to display a message to the user without waiting for the user to answer. I've done this by starting MessageBox.Show in a separate thread.

I also use a static DialogResult variable to allow me to see the user's result in my calling code. My method also accepts a MessageBoxButtons parameter that is then passed on to the MessageBox.Show method.

The only problem is that I will need to close the box later on by calling another method. I've tried Thread.Abort and Thread.Interrupt but neither close the dialog.

Here is my current code for clarity (all contained in static Log):
Expand|Select|Wrap|Line Numbers
  1. public static void writeMessage(string LogType, string Error, MessageBoxButtons buttonSet)
  2. {
  3.     MessageData data = new MessageData();
  4.     data.message = Error;
  5.     data.LogType = LogType;
  6.     data.buttons = buttonSet;
  7.     messageThread = new Thread(new ParameterizedThreadStart(Log.displayMessage));
  8.     messageThread.Start(data);
  9. }
  10.  
  11. public static void closeMessage()
  12. {
  13.     messageThread.Interrupt();
  14. }
  15.  
  16. private class MessageData
  17. {
  18.     public string message;
  19.     public string LogType;
  20.     public MessageBoxButtons buttons;
  21. }
  22.  
  23. private static void displayMessage(object data)
  24. {
  25.     MessageData d = (MessageData)data;
  26.     Log.lastMessageResult = DialogResult.None;
  27.     Log.lastMessageResult = MessageBox.Show(d.message, d.LogType, d.buttons);
  28. }
I haven't written any code that uses this yet so if anyone also has any ideas on how to get the same functionality in a different fashion please that could be helpful as well.

Thanks in advance,
Fr33dan
May 15 '09 #1
3 4862
Plater
7,872 Expert 4TB
You're already doing most of the work, just make your own dialog form then?
May 15 '09 #2
Fr33dan
57
I considered that but I'm afraid it would become unnecessarily complex when trying to implement the various button sets.
May 15 '09 #3
tlhintoq
3,525 Expert 2GB
Not really. I did my own messagebox control that includes parameters such as an int for how many seconds until autoclosing. This way I can pass text, icon and 25 to have it auto close after 25 seconds for example.
May 15 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dennis C. Drumm | last post: by
4 posts views Thread by Tressa | last post: by
2 posts views Thread by Nuno Magalhaes | last post: by
1 post views Thread by =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post: by
2 posts views Thread by Risa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.