Solve a cross-threading Exception in WinForms

后端 未结 6 1800
北荒
北荒 2020-12-01 17:14

Presently I\'m working with WinForms(in C#) and I have to run the application in the background. For this purpose I\'m using asynchronous. When I run the application it\'s s

6条回答
  •  盖世英雄少女心
    2020-12-01 17:45

    Updated from Invoke to begin Invoke

    // you can define a delegate with the signature you want
    public delegate void UpdateControlsDelegate();
    
    public void SomeMethod()
    {
        //this method is executed by the background worker
        InvokeUpdateControls();
    }
    
    public void InvokeUpdateControls()
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new UpdateControlsDelegate(UpdateControls));
        }
        else
        {
            UpdateControls();
        }
    }
    
    private void UpdateControls()
    {
        // update your controls here
    }
    

提交回复
热议问题