在处理大量数据时如何保持响应式UI? [英] How do I keep a responsive UI while processing large amounts of data?

查看:82
本文介绍了在处理大量数据时如何保持响应式UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建winform来处理(将txt文件转换为tiff)大量文件。我将所有代码放在按钮(btnProcess)后面。这是一个好主意吗?它可以工作,但是我注意到当我离开Winform并返回到该窗口时,我看到空白窗口,直到过程完成。我听说过后台工作者。后台工作者的目的是什么?

I am creating winform to process (convert txt files to tiff) large amount of files. I put all the code behind a button (btnProcess). Is this a good idea? It works but I noticed when I go away from the winform and come back to this I see blank window until the process is complete. I heard about background worker. what is the purpose of background worker?

推荐答案

这里需要的是多线程。这意味着两个(或更多)代码线程将并行运行。其中一个是UI线程,一个负责绘制窗口。对于您而言,您是在UI线程中运行代码,从而在代码运行时阻止了UI呈现。

What you need here is multi-threading. That means that two (or more) threads of code would run in parallel. One of them would be the UI thread, the one responsible for drawing the window. In your case you are running your code in the UI thread and thus blocking the UI rendering while your code is running.

BackgroundWorker 就是要在新线程上开始操作,这是您所需要的。

The purpose of the BackgroundWorker is to start an operation on a new thread and is what you need.

这篇关于在处理大量数据时如何保持响应式UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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