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:	108
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 CarlTrading, 03-31-2026, 09:41 PM
1 response
43 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 04-01-2026, 02:41 AM
0 responses
20 views
0 likes
Last Post CarlTrading  
Started by CaptainJack, 03-31-2026, 11:44 PM
0 responses
30 views
1 like
Last Post CaptainJack  
Started by CarlTrading, 03-30-2026, 11:51 AM
0 responses
47 views
0 likes
Last Post CarlTrading  
Started by CarlTrading, 03-30-2026, 11:48 AM
0 responses
38 views
0 likes
Last Post CarlTrading  
Working...
X