Donate

Pass By Reference Not Allowed In UI Threading Of Windows Forms In C#

The code below does not allow pass by reference to be used in UI threading
particularly method Invoker.
private void ShowPassMeta(ref int passedSound)  
  {  
       if (lblPassSoundex.InvokeRequired)  
       {  
            lblPassSoundex.Invoke((MethodInvoker)delegate  
            {  
                 ShowPass(ref passedSound);  
            });  
       }  
       else  
       {  
            lblPassSoundex.Text = (++passedSound).ToString();  
       }        
  }
The solution is to store the method parameter in another variable as shown below:
private void ShowPassMeta(ref int passedSound)  
  {        
    var passedSound1 = passedSound;  
    if (lblPassSoundex.InvokeRequired)  
    {  
      lblPassSoundex.Invoke((MethodInvoker)delegate  
      {  
        ShowPassMeta(ref passedSound1);  
      });  
    }  
    else  
    {  
      lblPassSoundex.Text = (++passedSound1).ToString();  
       }  
  }
Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.