InvokeRequired hangs

1k Views Asked by At

The UI thread hangs occasionally at the statement 'if (this.InvokeRequired)' in the following method.

Can you help me to identify the cause of the issue

  public void OnModuleInitializationCompleted(object sender, EventArgs e)
  {
    ModuleStatusWindow.Logger.LogMessage("OnModuleInitializationCompleted", LogMessageType.Information, "Received {0}", (sender as IModule).Name);
    if (this.InvokeRequired)
    {
      this.BeginInvoke(new ECEventsHandler(OnModuleInitializationCompleted), sender, e);
    }
    else
    {
      CheckIfAllModulesInitComplete();
    }
  }

  private void CheckIfAllModulesInitComplete()
  {
    ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Enter >>");
    this._moduleStatusGrid.DataSource = this._moduleDataList.ToArray();
    this._moduleStatusGrid.Invalidate();
    ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Updated grid control...");
    if (this._moduleDataList.Count(moduleData => !moduleData.IsInitOver) == 0)
    {
      this._footprint.DeActivate();
      ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Stopping message listenr...");
      ClientMessageListner.Stop();
      ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Closing Window...");
      this.Close();
    }
    ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Leave <<");
  }
3

There are 3 best solutions below

0
On

More than likely, you have some kind of race condition which is resulting in a deadlock. Or, your debugging information is messed up and that's not really the line that is blocking.

3
On

I don't think InvokeRequired is likely to hang. BeginInvoke might but I don't think it will.

Few ideas.

BeginInvoke is running correctly but the UI thread is busy so it never gets round to running OnModuleInitializationComplete. What does this thread go on to do? Does it begin a wait (like calling EndInvoke) as some point?

InvokeRequired is returning false and your CheckIfAllModulesInitComplete method is hanging.

I would add more logging to OnModuleInitializationComplete to show which path of the If it has taken, then update your question with the new information.
If you can also provide a little more detail of the code around this method it might be useful, especially anywhere that will wait for this method to complete.

0
On

I would add a log message after the InvokeRequired and before the BeginInvoke call.

I suspect it's the BeginInvoke blocking, because the UI thread is busy, perhaps because it's waiting for something else.