如何引发“应用程序无响应" [英] How to provoke "Application not responding"

查看:29
本文介绍了如何引发“应用程序无响应"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我需要编写一个程序,将著名的应用程序无响应"消息显示在屏幕上.我不知道我怎么会激怒它.

For testing purposes I need to write a program that will put the famous "Application not responding" message on screen. I don't know how I'd provoke that.

执行此操作的最快和最简单的方法是什么?最好是 C/C++ 或 Java

What is the quickest and easiest way to do this? Preferably either C/C++ or Java

推荐答案

在 Windows API 中,GUI 程序通过消息从操作系统获取输入,并且它必须以一种方式接收和发送这些消息消息循环.发生的任何事情(鼠标移动/点击、按键,以及退出应用程序的请求等)都会作为消息传递.

In the Windows API, a GUI program gets input from the OS through messages and it has to receive and dispatch those messages in a message loop. Anything happening (mouse movement/click, key press, but also things like a request to quit the application) is delivered as a message.

随着每个窗口的创建,您可以附加一个回调函数来实际处理与该窗口相关的消息(请注意,每个 GUI 元素都是一个 窗口,例如按钮或文本输入字段).此回调称为窗口过程 在 Windows API 中.

With every window created, you can attach a callback function that actually handles the messages relevant to that window (note that every GUI element is a window, like e.g. a button or a text input field). This callback is called a window procedure in the Windows API.

当应用程序未能及时处理消息时,操作系统会显示消息应用程序未响应".所以在你的代码中的某个地方,你只需要停止处理消息来激发它,例如,你可以故意在你的消息循环或窗口过程中进入一个无限循环.

The message "Application not responding" is shown by the OS when an application fails to process messages in time. So somewhere in your code, you just have to stop processing messages to provoke it, for example, you could intentionally enter an infinite loop in your message loop or in a window procedure.

现在,如果您使用 Java 编码或使用 C 中的某些 GUI 工具包(例如 GTK),您将不会编写自己的消息循环和窗口过程,框架/工具包为您提供这些,给您一个抽象高级接口.是否可以激起一个无响应的应用程序取决于框架的设计.您基本上需要占用UI 线程"(例如,在通常处理消息的主线程中执行无限循环,因此它无法完成其工作).

Now, if you're coding in Java or using some GUI toolkit (e.g. GTK) in C, you won't write your own message loop and window procedures, the framework/toolkit provides these for you, giving you an abstract higher-level interface. Whether or not you can provoke a non-responding application depends on the design of the framework. You basically need to hog the "UI Thread" (e.g. execute an infinite loop in the main thread that normally processes the messages, so it can't do its work).

如果由于应用程序有问题而显示此消息,则该错误正在主线程中执行以下操作:a) 无限循环,b) 无限阻塞或 c) 进入死锁.你永远不应该在 win32 应用程序的主/UI 线程中做一些不能及时完成的​​事情.或者,更一般地说,你永远不应该在任何 GUI 程序中处理事件/处理消息的线程中做类似的事情.

If this message is shown because of a buggy application, the bug is doing something in the main thread that a) loops infinitely, b) blocks infinitely or c) enters a deadlock. You should never do something in the main/UI thread of a win32 application that doesn't finish promptly. Or, more generally, you should never do something like that in the thread that handles events / processes messages in any GUI program.

这篇关于如何引发“应用程序无响应"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆