Progress reporting from background task

强颜欢笑 提交于 2020-01-15 12:15:37

问题


I have a background task that imports files into a database. I want the user to see what is currently happening (collecting files/ importing files), which file is currently processed and how far the task has progressed. How can I do this in an easy way? The interaction between Model and Controller is so close, that I could almost put the importing code into the window's code file and change the progress bar value etc. directly. What do you think? How would you solve this problem?


回答1:


Use a BackgroundWorker, it's perfect for this task. It can notify the UI of current progress using the ReportProgress method, which raises the ProgressChanged event on the UI thread (which means you don't have to worry about cross-thread calls and Invoke)




回答2:


Progress bars can be misleading in a lot of cases so I suggest you be careful setting expectations.

If you do end up showing progress as a percentage of some sort do account for varying load times depending on file size.



来源:https://stackoverflow.com/questions/1639969/progress-reporting-from-background-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!