我有一个后台任务,将文件导入数据库.我希望用户查看正在发生的事情(收集文件/导入文件),当前正在处理的文件以及任务已进行了多长时间.我该如何轻松地做到这一点? Model和Controller之间的交互是如此紧密,以至于我几乎可以将导入代码放入窗口的代码文件中,并直接更改进度条值等.你怎么认为?您将如何解决这个问题?
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?
推荐答案使用 BackgroundWorker ,非常适合此任务.它可以使用ReportProgress方法将当前进度通知UI,这将在UI线程上引发ProgressChanged事件(这意味着您不必担心跨线程调用和Invoke)
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)
更多推荐
后台任务的进度报告
发布评论