2

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 <<");
  }
Maanu
  • 5,093
  • 11
  • 59
  • 82
  • 2
    how specifically did you determine that it was hung there *in particular*? – Marc Gravell Apr 28 '11 at 11:18
  • From the log files, I could see the log entry before this InvokeRequired statement. But the log entries in CheckIfAllModulesInitComplete are not present in the log file – Maanu Apr 28 '11 at 11:26
  • Any chance its actually the call to the logger causing the hang? – BugFinder Apr 28 '11 at 11:28
  • @BugFinder: The logger module is pretty stable. I don't think the hang is in the logger – Maanu Apr 28 '11 at 11:30
  • I didnt mean that it was unstable, but maybe its causing the lock, and doesnt return as a result. Just a thought. Especially if its not always easy to track down – BugFinder Apr 28 '11 at 15:47

3 Answers3

1

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.

pipTheGeek
  • 2,703
  • 17
  • 16
  • I added source code of CheckIfAllModulesInitComplete in my first post. The first log entry in the method is not available in the log file – Maanu Apr 28 '11 at 11:43
  • Notice! InvokeRequired **might hang** because it lock current control. see `lock(this)` in the following URL [link] https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Control.cs,bf3e3af2a14ecf9f – AndersonMeng Jan 14 '19 at 03:13
  • Let's say, if there is another thread A, hold resource B first, and then call **InvokeRequired** (require lock **this**). At the same time, your main thread (UI thread may hold lock **this**) requires resource B. It comes a dead lock... – AndersonMeng Jan 14 '19 at 03:18
1

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.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
0

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.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291