nonexistent myth
nonexistent myth

Reputation: 135

Difference between Invoke and <Control>.Invoke

Winforms Application. Background thread retrieves messages from a MQ and makes changes on the UI Thread.

I need to call this method to Update a custom list

    private void UpdateList()
    {         
        if (ctrlLabel.IsHandleCreated)
        {
            ctrlLabel.Invoke(new Action(() =>
            {
                //Do Something
            }
            ));
        }

        ctrlListView.Data = package;

       //MARK
       ctrlListView.Invoke(new Action(() =>
        {
            ctrlListView.LoadData();
        }
        )); 
       //MARK           
    }

This method is invoked both times by the background thread. However, this works for the first call after the control is freshly instantiated.

On deleting items, when I try to refresh, this errors out with Null Exception Errors.

Interestingly, I was able to run this with some minor modification. I changed the [Control].Invoke to just Invoke, and it works(for the second call alone)

       //MARK
       Invoke(new Action(() =>
        {
            ctrlListView.LoadData();
        }
        )); 
       //MARK           

so, how does Invoke and [control].Invoke differ in operation?

Seen on Win7, .Net 4.0

Upvotes: 0

Views: 105

Answers (1)

Habib
Habib

Reputation: 223277

[control].Invoke calls specific to that particular control, if you use Invoke then it refers to this which is the current form.

So:

   Invoke(new Action(() => ....

the above equals to:

this.Invoke(new Action(() => ....

Where this is the current form.

On deleting items, when I try to refresh, this errors out with Null Exception Errors.

If you control is null because of the delete then you will get the NRE.

Upvotes: 2

Related Questions