I want to Display a messagebox with no buttons for 5 secs in a WPF application, where the text in the message box will change every sec, like in the first sec it will be "5 sec remaining" the next will be "4 sec remaining",... and when it reaches zero then the messagebox will disappear. So if anyone could please advise how to do this.
Asked
Active
Viewed 8,195 times
0
-
Stack Overflow is not a code writing service. You need to have a go at a solution and post any issues you have with the code you've written. – Enigmativity May 27 '14 at 01:48
-
Thanks for your comment. I totally understand, but the thing I'm very new with WPF and I'm asking for assistance as I believe its not a very complicated question, and that's why I'm asking for assistance from people with more experience than mine. – Tak May 27 '14 at 01:51
-
1If it is not complicated, and you're new to the technology, why not try it by yourself? You'll learn a lot more things by trying stuff than copy-pasting. The very essence of programming is experimentation. – Pierre-Luc Pineault May 27 '14 at 02:08
-
I've updated the question with a solution which is not working. – Tak May 27 '14 at 02:22
-
Answer was found [here][1] Thanks for the suggestions! [1]: http://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds – Tak May 29 '14 at 04:59
2 Answers
1
I've followed the solution found here
A class was created shown below:
class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow(null, _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
then it was called using :
AutoClosingMessageBox.Show("waiting", "wait...", 1000);
0
This isn't the most elegant solution but it should at least get your started:
public partial class MessageWindow : Window
{
private MessageWindow()
{
InitializeComponent();
}
public static void Show(string text, string caption, int timeout)
{
var msgWindow = new MessageWindow()
{
Title = caption
};
Task.Factory.StartNew(() =>
{
for (var n = 0; n < timeout; n++)
{
msgWindow.Dispatcher.Invoke(() =>
{
msgWindow.text.Text = string.Format("{0}{1}{2}s left...", text, Environment.NewLine, timeout - n);
});
System.Threading.Thread.Sleep(1000);
}
msgWindow.Dispatcher.Invoke(msgWindow.Close);
});
msgWindow.ShowDialog();
}
}

SKall
- 5,234
- 1
- 16
- 25