Automating the InvokeRequired code pattern

后端 未结 9 1193
醉话见心
醉话见心 2020-11-21 23:57

I have become painfully aware of just how often one needs to write the following code pattern in event-driven GUI code, where

private void DoGUISwitch() {
           


        
9条回答
  •  情话喂你
    2020-11-22 00:16

    I Kind of like to do it a bit different, i like to call "myself" if needed with an Action,

        private void AddRowToListView(ScannerRow row, bool suspend)
        {
            if (IsFormClosing)
                return;
    
            if (this.InvokeRequired)
            {
                var A = new Action(() => AddRowToListView(row, suspend));
                this.Invoke(A);
                return;
            }
             //as of here the Code is thread-safe
    

    this is a handy pattern, the IsFormClosing is a field that i set to True when I am closing my form as there might be some background threads that are still running...

提交回复
热议问题