gpt4 book ai didi

c# - 如何防止窗口标题栏上出现“无响应”消息?

转载 作者:太空狗 更新时间:2023-10-29 20:21:17 25 4
gpt4 key购买 nike

我使用 VS2010 和 C# 构建桌面应用程序。这个应用程序有一个表单,其中包含一项需要花费大量时间才能完成的庞大任务。初始化此表单后,它可以正常工作,除了在标题栏上显示 “无响应”,如图所示:

enter image description here

完成所有任务后,它会显示所需的输出。为什么会显示此消息,我该如何阻止它?

最佳答案

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

来自 MSDN MSDN 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.

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.

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.

关于c# - 如何防止窗口标题栏上出现“无响应”消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11025829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com