Windows 窗体:进度条无响应 [英] Windows Forms: Progress Bar Unresponsive

查看:40
本文介绍了Windows 窗体:进度条无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小应用程序可以转换多种文件格式,主窗口窗体包含多个文本框、按钮等.我有另一个文档"类处理所有实际转换工作,我的窗体基本上是迭代的通过所有文件,并调用适当的 Document 方法来转换文件.但是,在发生这种情况时,表单会停止响应.我创建了另一个带有进度条和取消按钮的简单表单,当转换开始向我们的(容易慌乱的)用户提供一些反馈时会生成该按钮.然而,当新表单加载时,所有控件都是白框,并且它也停止响应.转换完成后,进度条会正常关闭,主窗体再次响应.

I have a small application to convert several file formats, with a main windows form which contains several text boxes, buttons, etc. I have another "Document" class which handles all of the actual conversion work, and my form essentially iterates through all of the files, and calls the proper Document methods to convert the files. While this is happening, however, the form stops responding. I created another simple form with a progress bar and a cancel button to spawn when the conversion starts to provide some feedback to our (easily rattled) users. When the new form loads, however, all of the controls are white boxes, and it too stops responding. Once the conversion completes, the progress bar closes properly, and the main form becomes responsive again.

有没有什么简单的方法可以让这两个表单相互独立,这样进度条即使在另一个表单没有响应的情况下也能运行?

Is there a simple way to make these two forms independent, so that the progress bar can operate even while the other form is unresponsive?

推荐答案

最简单的解决方案是使用 BackgroundWorker 组件.您可以从 VS 工具箱中将其拖入.它允许您使用进度通知更新 UI 线程,以便您可以更新进度条以显示实际值(这比拥有选取框"样式的进度条更方便用户使用).

The simplest solution is to have your processing done on a background thread using the BackgroundWorker component. You can drag it in from the VS toolbox. It allows you to update the UI thread with progress notifications so you can update your progress bar to show realistic values (something much more user-friendly than having a "marquee" style progress bar).

这篇关于Windows 窗体:进度条无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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