locked
close form by code after display delay message RRS feed

  • Question

  • Hi All.

    I developed code to delay message.

    txtDelay.Text = "Data successfully updated.";
    var timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(3);
    timer.Tick += delegate
    {
        txtDelay.Text = String.Empty;
    };
    timer.Start();
    

    Now when click button Update the data is updated and form is close. I would like to close the form after delay message. How to modify code for that?

    Thanks.

    Monday, June 22, 2020 7:28 PM

All replies

  • Use this dialog which has a delay. Pick your time out, here it's six seconds.

    AutoClosingMessageBox.Show(
        "Update is done\time out in six seconds then close the app.",
        "delay",
        6000);
    
    Close();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by Naomi N Monday, June 22, 2020 8:03 PM
    Monday, June 22, 2020 7:56 PM
  • Hi Karen.

    Do you have idea how to modify my code instead to install plug-in application?

    Thanks

    Monday, June 22, 2020 9:28 PM
  • Hi Karen.

    Do you have idea how to modify my code instead to install plug-in application?

    Thanks

    I gave you the best solution which provides complete source code. Other than that you would need to create your own dialog with a timer or yet another library (which is overkill for this) found here.

    Or back to the author of the first solution, their first run at the auto close dialog.

    var userResult = AutoClosingMessageBox.Show("Yes or No?", "Caption", 1000, MessageBoxButtons.YesNo);
    if(userResult == System.Windows.Forms.DialogResult.Yes) { 
        // do something
    }
    ...
    public class AutoClosingMessageBox {
        System.Threading.Timer _timeoutTimer;
        string _caption;
        DialogResult _result;
        DialogResult _timerResult;
        AutoClosingMessageBox(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None) {
            _caption = caption;
            _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                null, timeout, System.Threading.Timeout.Infinite);
            _timerResult = timerResult;
            using(_timeoutTimer)
                _result = MessageBox.Show(text, caption, buttons);
        }
        public static DialogResult Show(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None) {
            return new AutoClosingMessageBox(text, caption, timeout, buttons, timerResult)._result;
        }
        void OnTimerElapsed(object state) {
            IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox
            if(mbWnd != IntPtr.Zero)
                SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            _timeoutTimer.Dispose();
            _result = _timerResult;
        }
        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);
    }
    https://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, June 22, 2020 10:18 PM