using System; using System.Windows.Forms; public class AutoClosingMessageBox { private System.Threading.Timer _timeoutTimer; private string _caption; private const int WM_CLOSE = 16; private AutoClosingMessageBox(string text, string caption, int timeout) { this._caption = caption; this._timeoutTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.OnTimerElapsed), null, timeout, -1); MessageBox.Show(text, caption); } public static void Show(string text, string caption, int timeout) { new AutoClosingMessageBox(text, caption, timeout); } private void OnTimerElapsed(object state) { IntPtr intPtr = AutoClosingMessageBox.FindWindow(null, this._caption); if (intPtr != IntPtr.Zero) { AutoClosingMessageBox.SendMessage(intPtr, 16u, IntPtr.Zero, IntPtr.Zero); } this._timeoutTimer.Dispose(); } [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); }