简体   繁体   English

如何立即完全停止应用程序并显示消息?

[英]How to immediately completely stop an application and show a message?

When my application needs to make an emergency stop, I could call Halt or ExitProcess to terminate it immediately.当我的应用程序需要紧急停止时,我可以调用HaltExitProcess来立即终止它。 This shouldn't happen quietly, though;不过,这不应该悄然发生。 the user needs to be shown a message.需要向用户显示一条消息。 Therefore I looked to FatalAppExit but was surprised that it keeps everything (timers, threads) running until the user has closed the dialog.因此,我查看了FatalAppExit但很惊讶它会一直运行所有内容(计时器、线程),直到用户关闭对话框。

I understand that showing a UI owned by the process requires the process to keep running.我知道显示进程拥有的 UI 需要进程继续运行。 The UI does not need to be owned by my process, however.但是,UI 不需要归我的流程所有。 The dialog presented by FatalAppExit seems to be owned by Windows, even though it keeps the process running as well. FatalAppExit显示的对话框似乎归 Windows 所有,即使它也保持进程运行。

The main problem with FatalAppExit is that it's blocking—I want the dialog but not wait for confirmation—so I still can't terminate right after. FatalAppExit的主要问题是它被阻塞了——我想要对话但不等待确认——所以我仍然无法立即终止。 To circumvent this, I'd have to start a thread that calls FatalAppExit , wait for a bit and then terminate the process.为了避免这种情况,我必须启动一个调用FatalAppExit的线程,稍等FatalAppExit ,然后终止该进程。 Not liking the race condition there.不喜欢那里的比赛条件。

I could also launch a process of my own to show the message, but I'd rather not if Windows can handle it.我也可以启动我自己的进程来显示消息,但如果 Windows 可以处理它,我宁愿不这样做。

Does Windows provide means to both terminate and show a friendly message at the same time? Windows 是否提供同时终止和显示友好消息的方法? Any other suggestions to handle this as cleanly as possible?还有其他建议可以尽可能干净地处理这个问题吗?

As far as I know the only option here is to start an external process that displays the message and terminate immediately.据我所知,这里唯一的选择是启动一个显示消息并立即终止的外部进程。 You could write your own program for that purpose (which might be the easiest option) or you could start a batch file like this:您可以为此目的编写自己的程序(这可能是最简单的选择),或者您可以像这样启动一个批处理文件:

msg "%USERNAME%" %*

This sends a message to the given user (%USERNAME% is the currently logged on user) and uses all parameters passed to it as the actual message.这会向给定用户(%USERNAME% 是当前登录的用户)发送一条消息,并使用传递给它的所有参数作为实际消息。

Personally I would write my own program, so I could write a log entry and do other stuff if it becomes necessary later.就我个人而言,我会编写自己的程序,因此我可以编写日志条目并在以后需要时做其他事情。

You can have a global variable, such as你可以有一个全局变量,比如

var ShutdownMessage: string;

When you need to terminate with an error, do:当您需要以错误终止时,请执行以下操作:

ShutdownMessage := 'Error occured';
Application.Terminate;

and in the dpr file after Application.Run:在 Application.Run 之后的 dpr 文件中:

if ShutdownMessage <> '' then
  MessageBox(0, PChar(ShutdownMessage), 'Error', MB_OK or MB_ICONERROR);

Then the message will be shown after all forms closed and main loop exited.然后将在所有表单关闭并退出主循环后显示该消息。

Or, if that fails, you can try to raise an exception after Application.Run - it should be propagated to OS或者,如果失败,您可以尝试在 Application.Run 之后引发异常 - 它应该传播到操作系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM