更新时间:2023-02-20 22:18:37
您应该真正将长时间运行的非UI代码移到单独的线程中,而不要使用ProgressBar
.在WinForms中执行此操作的标准且更简单的方法是使用BackgroundWorker
组件,该组件可以引发ProgressChanged
事件,您可以在其中更新您的UI.重要的是要注意,ProgressChanged
事件是在UI线程上引发的,而不是在工作线程上引发的,因此,您甚至不需要使用Invoke()
来执行UI操作(例如更新ProgressBar).
Instead of the ProgressBar
, you should really move your long-running, non-UI code into a separate thread. The standard and easier way of doing this in WinForms is to use BackgroundWorker
component, which can raise ProgressChanged
event where you can update your UI. Important to note that ProgressChanged
event is raised on the UI thread, not on the worker thread, so you don't even need to use Invoke()
to perform UI operations (such as updating your ProgressBar).