拆解一个MessageBox编程而无需用户输入用户、MessageBox

2023-09-03 17:39:42 作者:回眸﹌泪倾城

我使用了一个的MessageBox 不时弹出用户警报消息。我的特定的应用程序可以被设置,以便有次,其中用户是在计算机和其他时间,其中用户可能无法在计算机中的小时甚至几天的前面的前面到远程运行。有时我会弹出使用的MessageBox 警报消息,但一段时间后,警报不再相关。例如,我弹出是因为没有满足某些条件​​的任务不能完成警报。几分钟后,该标准被满足,任务开始。这的MessageBox 不再适用。

I'm using a MessageBox from time to time to pop up alert messages for the user. My particular application can be setup to run remotely so there are times where the user is in front of the computer and other times where the user may not be in front of the computer for hours or even days. Sometimes I popup an alert message using MessageBox but after some period of the time the alert is no longer relevant. For example, I popup an alert that a task can't be completed because of some criteria not being met. A few minutes later that criteria is met and the task begins. That MessageBox is no longer relevant.

我希望能够以编程方式关闭的MessageBox 在这些情况下,该消息已不再相关。这可能吗?目前,我在一个线程中使用创建我的的MessageBox 目标:

I want to be able to programmatically close the MessageBox in these cases where the message is no longer relevant. Is this possible? Currently I create my MessageBox objects in a thread using:

new Thread(() => MessageBox.Show("Some text", "Some caption")).Start();

我这样做,这样应用程序可以在后台继续工作,而不会被停止了的MessageBox 。有什么建议?

推荐答案

为什么不把自定义的消息框?你可以有它显示一个固定的时间长度或延迟到您的应用程序通过code将其关闭。

Why not make a custom message box? You could have it display for a fixed amount of time or until your app closes it through code.

创建自定义的消息框(Form类的子女)的实例,并将其保存为一个变量(例如, MyMessageBox ),然后用 MyMessageBox.Show(); 。当你想要把它记下来,​​调用 MyMessageBox.Close();

Create an instance of your custom message box (child of Form class) and save it as a variable (ex. MyMessageBox), then show it with MyMessageBox.Show();.When you want to take it down, call MyMessageBox.Close();

如果您有问题,关闭它,如果你在另一个线程,尝试调用 MyMessageBox.Invoke打开它(新动作(()=> {MyMessageBox.Close();})); 将运行命令 MyMessageBox.Close(); 在同一个线程 MyMessageBox 是在创建,以不引起问题。

If you have problems closing it if you opened it in another thread, try calling MyMessageBox.Invoke(new Action(() => {MyMessageBox.Close();})); That will run the command MyMessageBox.Close(); on the same thread MyMessageBox was created in, as to not cause issues.