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
Avoid calling Application.DoEvents. Often times this leads to more problems than it solves. It is generally considered bad practice because there exists better alternatives which keep the UI pumping messages.
Avoid changing setting CheckForIllegalCrossThreadCalls = false. This does not fix anything. It only masks the problem. The problem is that you are attempting to access a UI element from a thread other than the main UI thread. If you disable CheckForIllegalCrossThreadCalls then you will no longer get the exception, but instead your application will fail unpredictably and spectacularly.
From within the DoWork event handler you will want to periodically call ReportProgress. From your Form you will want to subscribe to the ProgressChanged event. It will be safe to access UI elements from within the ProgressChanged event handler because it is automatically marshaled onto the UI thread.