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

前端 未结 3 1018
野性不改
野性不改 2020-12-06 13:24

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 noti

相关标签:
3条回答
  • 2020-12-06 14:00

    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 讨论(0)
  • 2020-12-06 14:08

    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.

    0 讨论(0)
  • 2020-12-06 14:12

    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.

    0 讨论(0)
提交回复
热议问题