How to use multithreading with Winform?

前端 未结 9 1309
无人及你
无人及你 2020-12-17 16:27

I\'m a newbie with multithreading. I have a winform that have a label and a progress bar.

I wanna show the processing result. Firstly, I use Application.DoEven

9条回答
  •  無奈伤痛
    2020-12-17 16:50

    There are some fundamental problems with the code you have shown. As other have mentioned, Application.DoEvents() will not do what you want from a background thread. Separate your slow background processing into a BackgroundWorker and update your progress bar in the UI thread. You are calling progressBar1.Value++ from a background thread, which is wrong.

    Never call Control.CheckForIllegalCrossThreadCalls = false, that will only hide errors.

    If you want to update a progress bar from a background thread, you will have to implement a ProgressChanged handler; you are not doing that.

    If you need an example of implementing a BackgroundWorker, consult the article BackgroundWorker Threads from Code Project.

提交回复
热议问题