0

I am trying to Update UI using different threads and use the below process to do so.But getting the above error during invoke. Kindly advice is this not allowed.

    delegate void SetLabelCallback(string text,string Qmgr);
    private void Set_status(string text, string Qmgr)
    {
        if (this.Status1A.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(record_count);
            this.Invoke(d, new object[] { text,Qmgr });
        }
        else
        {
            switch (Qmgr)
            {
                case "GCSSPR1A": this.Status1A.Text = text;
                    break;
                case "GCSSPR1B": this.B1_Status.Text = text;
                    break;
                case "GCSSPR2A": this.A2_Status.Text = text;
                    break;
                case "GCSSPR2B": this.B2_Status.Text = text;
                    break;
                case "GCSSPR3A": this.A3_Status.Text = text;
                    break;
                case "GCSSPR3B": this.B3_Status.Text = text;
                    break;
            }

        }
user2772983
  • 43
  • 2
  • 7

2 Answers2

1

I would also do it similarly to Baldrick.

He is using lambda expression and maybe you would use something like this

private void Set_status(string text, string Qmgr)
{ 
    if (this.InvokeRequired)
    {
    this.Invoke(new ReceivedEventHandler(Set_status), new Object[] {text, Qmgr});                
    }
    else 
    {
    }
}

But, I don't think that is the problem.

I received this problem previously when there was a mismatch between the number of parameters in the delegate handler/function call and the number of objects defined in the Invoke declaration.

this.Invoke(d, new object[] { text, Qmgr, something_missing });

I hope that helps.

CSharper
  • 81
  • 7
  • "mismatch between the number of parameters in the delegate handler/function call and the number of objects defined in the Invoke declaration." + 1 – Dave Dec 01 '14 at 15:30
0

Try changing the top of your function to something like this:

private void Set_status(string text, string Qmgr)
{
    if (this.Status1A.InvokeRequired)
    {
        this.Invoke((Action)(() => Set_status(text, Qmgr)));
    }
    else
    {

This way you shouldn't need the delegate declaration, etc.

Baldrick
  • 11,712
  • 2
  • 31
  • 35