开发者

How do I keep a responsive UI while processing large amounts of data?

开发者 https://www.devze.com 2023-02-01 13:57 出处:网络
I am creating winform to process (convert txt fil开发者_开发百科es 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 g

I am creating winform to process (convert txt fil开发者_开发百科es 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?


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.

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


BackgroundWorker class

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

The page I linked above contains a complete BackgroundWorker example.


It depends on your application. If this is a single purpose application that is not extremely long and the only problem is the screen doesn't paint. Which is what it sounds like to me, just throw an Application.DoEvents into the loop and be done with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号