如何防止一个窗口的标题栏上没有响应的消息? [英] How to prevent a Not Responding message on a window's title bar?

查看:235
本文介绍了如何防止一个窗口的标题栏上没有响应的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用VS2010和C#来构建一个桌面应用程序。此应用程序有一个窗体与一个巨大的任务,需要花费大量的时间来完成。当这种形式被初始化它完美的作品,但它显示的没有响应的标题栏,像图为:

I use VS2010 and C# to build a desktop application. This application has one form with a huge task which takes a lot of time to complete. When this form is initialized it works perfectly, except it shows "Not Responding" on the title bar, like the picture shows:

完成所有任务后,它显示所需的输出。为什么显示这个消息,我该如何预防呢?

After completing all tasks, it shows the desired output. Why is this message shown, and how do I prevent it?

推荐答案

您需要使用一个BackgroundWorker这样耗时的任务将在一个单独的线程异步运行。这将允许Windows多任务处理,使UI响应。你应该使用一个等待光标或其他一些视觉指示器让用户知道你的应用程序正忙

You need to use a BackgroundWorker so that the time consuming task will run in a separate thread asynchronously. That will allow Windows multitasking to make the UI responsive. You should use a wait cursor or some other visual indicator to let the user know that your application is busy.

从MSDN的 MSDN的BackgroundWorker

BackgroundWorker的类允许您在一个单独的,专门的线程运行的操作。例如下载和数据库事务耗时的操作可能会导致用户界面(UI),以看起来好像它已经停止,而他们正在运行的响应。当你想要一个负责任的UI,你都面临着这样的操作相关的长时间延误,BackgroundWorker的类提供了一个方便的解决方案。

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

要执行耗时的操作背景,创建一个BackgroundWorker和侦听报告您的操作进度,当你操作完成信号的事件。您可以通过编程创建BackgroundWorker的,也可以从工具箱的组件选项卡将其拖动到您的窗体。如果您在Windows窗体设计器创建BackgroundWorker的,它会出现在组件栏中,其属性将显示在属性窗口。

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox. If you create the BackgroundWorker in the Windows Forms Designer, it will appear in the Component Tray, and its properties will be displayed in the Properties window.

要为建立后台操作,添加事件处理程序DoWork的事件。在调用这个事件处理程序的耗时的操作。若要启动操作,调用的RunWorkerAsync。要接收进度更新通知,处理ProgressChanged事件。要接收通知操作完成时,处理RunWorkerCompleted事件。

To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

这篇关于如何防止一个窗口的标题栏上没有响应的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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