What is the best way to update form controls from a worker thread?

前端 未结 4 1513
醉酒成梦
醉酒成梦 2020-12-18 04:47

I\'ve done some research and I can\'t really find a preferred way to do updating of form controls from a worker thread in C#. I know about the BackgroundWorker component, b

4条回答
  •  鱼传尺愫
    2020-12-18 05:14

    I would also consider InvokeRequired (VS2008 only) when calling Invoke. There are times that you will not be updating the UI from a seperate thread. It saves the overhead of creating the delegate etc.

    if (InvokeRequired)
            {
                //This.Invoke added to circumvent cross threading exceptions.
                this.Invoke(new UpdateProgressBarHandler(UpdateProgressBar), new object[] { progressPercentage });
            }
            else
            {
                UpdateProgressBar(progressPercentage);
            }
    

提交回复
热议问题