C# WinForm 提示框延迟自动关闭

有时候我们需要弹出个提示框然后让它自己关闭,然而实际使用中的弹出框确实阻塞进程,网上貌似有一种另类的解决方式,大致思路是把弹出框放到另外的一个窗体上,直接贴代码

主窗体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Windows.Forms;
 
namespace WinForm
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //延迟800毫秒关闭的信息框
            MessageBox.Show(new DelayCloseForm(800), "执行成功!");
        }
    }
}

  

起到延迟作用的窗体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace WinForm
{
    public partial class DelayCloseForm : Form
    {
        public DelayCloseForm(int interval = 500)
        {
            InitializeComponent();
            //计时器
            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();
        }
    }
}

  

posted @   WmW  阅读(5015)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示