C# WinForm prompt box delays automatic closing

Sometimes we need to pop up a prompt box and let it close by itself. However, the pop-up box in actual use does block the process. There seems to be an alternative solution on the Internet. The general idea is to put the pop-up box on another form and directly paste code

main form

using System;
using System.Windows.Forms;

namespace WinForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Delay 800 milliseconds to close the information box
            MessageBox.Show(new DelayCloseForm(800), "Successful execution!");
        }
    }
}

  

form that delays

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinForm
{
    public partial class DelayCloseForm : Form
    {
        public DelayCloseForm(int interval = 500)
        {
            InitializeComponent();
            // timer
            this.components = new Container();
            Timer timer1 = new Timer(this.components);
            timer1.Enabled = true;
            timer1.Interval = interval;
            timer1.Tick += timer1_Tick;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324500099&siteId=291194637