向父 UI 线程报告进度
如果我没有清楚地解释这一点,我很抱歉,但我正在编写一个应用程序,它给我带来了一些线程问题。
我有一个启动 System.Timers.Timer 的 UI。每次该计时器过去时,都会触发一个工作流程,该工作流程会打开一个进度屏幕。为了防止在最后一个工作流程完成之前启动另一个工作流程,它会锁定主窗体上的一个对象。 此进度屏幕启动并报告使用 FileCopyEX 进行某些文件复制的进度。
我遇到的问题是,直到工作流程完成后才会显示进度屏幕。
希望这会让它更清楚:
主表格 | 计时器已过 | 工作流程开始 | 进度屏幕打开(发生的错误返回到上一个) | 发生文件复制(进度报告回进度屏幕) 如果没有错误,则在下一个刻度之前返回主屏幕。
到目前为止,我只实现了非常简单的线程,所以我不确定如何最好地实现它。我尝试在 BackGroundWorker 上启动工作流程以将其与 UI 线程分开,但其行为相同。
谢谢
apologies if I don't explain this clearly, but I'm writing an app which is causing me some problems with threading.
I have a UI which starts a System.Timers.Timer. Each time this timer elapses it triggers a workflow which opens a progress screen. To prevent another workflow starting before the last one has finished it locks an object on the main form.
This progress screen starts and reports the progress of, some file copying using FileCopyEX.
The problem I'm having is that the progress screen does not display until after the work flow has been completed.
Hopefully this will make it clearer:
Main Form
|
Timer Elapses
|
WorkFlow Starts
|
Progress Screen opens (errors which occur go back to the previous)
|
File copying occurs (progress reported back to progress screen)
If no errors, returns to main screen before next tick.
Until now I've only implemented very simple threading, so I'm not sure how best to implement this. I've tried starting the workflow on a BackGroundWorker to seperate it from the UI thread but it behaves the same.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您阅读有关BackgroundWorker 的内容。这有报告进度的钩子。
请记住,系统计时器在非 UI 线程上调用其事件处理程序。每当您使用 UI 执行某些操作时,您都必须在 UI 线程上。
I suggest that you read about the BackgroundWorker. This has hooks for reporting progress.
Remember that system timers invoke its event handler on a non-UI thread. Whenever, you do something with the UI you must be on the UI thread.
如果没有代码,我只能猜测原因,但可能的原因是“进度”窗口需要 UI 泵送消息才能出现 - 即 UI 线程需要运行。
如果 UI 线程正忙于运行您的工作流程,那么在此之后它才会开始处理窗口的显示。您需要分离流程,以便进度窗口位于 UI 线程上,而工作流程位于后台线程上。
希望这是有道理的!
Without the code I can only guess at the cause, but the likely reason is that the Progress window needs the UI to be pumping messages to appear - i.e. the UI thread needs to be running.
If the UI thread is busy running your workflow, then it won't get around to processing the displaying of your window until after that. You need to separate your flow so that the progress window is on your UI thread and the workflow is on a background thread.
Hope that makes sense!