Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Message Box with timeout using NTMessageBoxSimple

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Message Box with timeout using NTMessageBoxSimple


    I have seen several requests for timeout message box functionality.

    Just sharing some code used in a recent project.


    Click image for larger version

Name:	xx.png
Views:	95
Size:	4.6 KB
ID:	1340041

    If timeout happens Rtn.ToString() will be "Cancel"

    Also check out the help text article on "NTMessageBoxSimple" if you need help finding the window object


    Code:
            Thread th = new Thread(() => {
                                    
                string Txt = "Message text to display";
                string Cap = "Message box caption";
                            
                NtMsgResult Mb = new NtMsgResult();
                MessageBoxResult Rtn = Mb.Show(Window,Txt,Cap,10000);
                
                Print(Rtn.ToString() );
            
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
            ​


    Code:
    
            public class NtMsgResult
            {
                #region Class vars
                private string _caption;
                private System.Threading.Timer _timeoutTimer;
                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);
                #endregion
                
                public NtMsgResult(){}
                
                public MessageBoxResult Show(Window win, string text, string caption, int timeout)
                {
                    return Show(win, text, caption, timeout, MessageBoxButton.YesNo, MessageBoxImage.Question) ;
                }
                
                public MessageBoxResult Show(Window win, string text, string caption, int timeout, MessageBoxButton Btns, MessageBoxImage Img)
                {
                    _caption = caption;
                    _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                        null, timeout, System.Threading.Timeout.Infinite);
                    using(_timeoutTimer)
                    {
                        MessageBoxResult Rtn = NTMessageBoxSimple.Show(win,text,caption,Btns,Img);
                        return Rtn;
                    }                
                }
                
                void OnTimerElapsed(object state) {
                    IntPtr mbWnd = FindWindow(null, _caption); // lpClassName is #32770 for MessageBox
                    if(mbWnd != IntPtr.Zero)
                        SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                    _timeoutTimer.Dispose();
                }
    
            }
    ​
    ​

Latest Posts

Collapse

Topics Statistics Last Post
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
0 responses
557 views
0 likes
Last Post Geovanny Suaza  
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
0 responses
324 views
1 like
Last Post Geovanny Suaza  
Started by Mindset, 02-09-2026, 11:44 AM
0 responses
101 views
0 likes
Last Post Mindset
by Mindset
 
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
0 responses
545 views
1 like
Last Post Geovanny Suaza  
Started by RFrosty, 01-28-2026, 06:49 PM
0 responses
547 views
1 like
Last Post RFrosty
by RFrosty
 
Working...
X