1

The following method will be invoked from a non UI thread. Should I check InvokeRequired, for calling these items in the method?

a. this._moduleStatusGrid.Invalidate()
b. this.Close()

private void CheckIfAllModulesInitComplete()
      {
        this._moduleStatusGrid.Invalidate();
        if (this._moduleDataList.Count(moduleData => !moduleData.IsInitOver) == 0)
        {
          this._footprint.DeActivate();
          this.Close();
        }
      }
Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
Maanu
  • 5,093
  • 11
  • 59
  • 82

2 Answers2

3

Control.Invoke and Control.BeginInvoke are safe to call from the UI thread and non-UI threads, so if you already know you are on a non-UI thread there is no harm (IMO) skipping the check and just calling Invoke/BeginInvoke.

Example:

anyControl.Invoke((MethodInvoker)delegate{
    // anything to run on UI thread here
});
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • This saved me a ton of refactoring on a problem I've been pulling my hair out over for a couple of days. Thanks! – delliottg May 20 '16 at 18:00
0

It sounds like you could be asking one of the following

  1. Given that this method runs in a background thread, within the methods Invalidate and Close should I be checking the InvokeRequired property?
  2. Given that this method runs in a background thread the InvokeRequired property will always return false so should I just avoid checking it?

For #1 the answer is no. The methods Close and Invalidate don't have a responsibility to check the InvokeRequired property. It is an implicit part of their contract that the InvokeRequired property be false before they are called.

For #2, yes if it's always called on a background thread I would skip the check and just go straight to the Invoke methods.

In either case I would rewrite the method as follows.

private void CheckIfAllModulesInitComplete()
{
  MethodInvoker del = delegate {
    this._moduleStatusGrid.Invalidate();
    if (this._moduleDataList.Count(moduleData => !moduleData.IsInitOver) == 0)
    {
      this._footprint.DeActivate();
      this.Close();
    }
  };
  this.Invoke(del, null);
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • @JaredPar- Thanks.. Just to know - What would happen if this._moduleStatusGrid.Invalidate() or this.Close() is called direcly from the callback thread? – Maanu May 02 '11 at 14:59
  • @Maanu, bad things. Occasionally, in some .Net versions, the app will continue running with no apparent issue. But it doesn't mean the operation is any safer. Calling these methods from a BG thread is simply dangerous and will cause issues down the road – JaredPar May 02 '11 at 15:02