C#自动关闭MessageBox的方法
前言
在winform或WPF软件开发过程中会遇上软件初始化需要加载各类配置文件或者调用各种方法的情况,此时调用的方法可能存在MessageBox,窗口弹出后需要人工干预才能关闭,本文描述了C#开发过程中MessageBox自动关闭的方法一、方法
1、在MessageBox打开前,开启定时器
2、程序运行至MessageBox开启
3、定时器时间到达,Time_Tick内运行FindWindow方法,找到MessageBox窗口,并将其关闭
4、关闭定时器
二、步骤
1.引入库
代码如下(示例):
using System.Runtime.InteropServices;
using System.Windows.Threading;
其中
System.Runtime.InteropServices 用于FindWindow函数
System.Windows.Threading 用于定时器Timer类,可根据实际情况替换,此处对应为WPF下的DispatcherTimer类
2.开启定时器
代码如下(示例):
DispatcherTimer timer = new DispatcherTimer();
timer.Inter