Form Hangs when processing a callback event from another module
<p>We have a console application named EC. We got a recent requirement from the customer to show a form from this console application. We have displayed the form with the following source code</p>

<pre><code>ModuleInitializerForm moduleInitializerDlg = new ModuleInitializerForm()
{
  Parent = parent,
  TopMost = true,
  TopLevel = true,
  Text = Common.MESSAGE_INFO_TITLE,
  ControlBox = false,
  FormBorderStyle = FormBorderStyle.FixedDialog,
  KeyPreview = false,
};

moduleInitializerDlg.Initialize();
moduleInitializerDlg.ShowDialog();   
</code></pre>

<p>The purpose of this form is to display the module initialization status. The form will receive module initialization status as events from other components. It automatically closes, when it receives the last module initialization completed/failed event. The source code of the form is given below.</p>

<p>The issue is sometimes the OnModuleInitializationCompleted method will block with the following log entries. The first log entry is from the event callback thread and the second log entry is from the form thread. <strong>As per the source code, the method CheckIfAllModulesInitComplete should be invoked. But it is not happening.  Can you help me to solve the problem?</strong></p>

<p>[32][4/26/2011 17:43:15:828]-INFO-[ThreadId = 5332, OnModuleInitializationCompleted]-Received STAGE<br>
[33][4/26/2011 17:43:15:828]-INFO-[ThreadId = 6132, OnModuleInitializationCompleted]-Received STAGE</p>

<h2>Form Source Code</h2>

<pre><code>internal partial class ModuleInitializerForm : Form
{
  private List&lt;ModuleData&gt; _moduleDataList = new List&lt;ModuleData&gt;();
  private List&lt;MessageData&gt; _messageList = new List&lt;MessageData&gt;();

  delegate void ECEventsHandler(object sender, EventArgs e);

  public ModuleInitializerForm()
  {
    InitializeComponent();
    InitializeFootPrintControl();
  }

  internal void Initialize()
  {

    IEquipmentControl equipMentControl = null;
    ConnectToEquipmentControl(out equipMentControl);

    ReadOnlyCollection&lt;IModule&gt; moduleList = equipMentControl.GetAllModules();
    foreach (IModule module in moduleList)
    {
      this._moduleDataList.Add(new ModuleData(module));
      module.InitializationStarted += new EventHandler(OnModuleInitializationStarted);
      module.InitializationCompleted += new EventHandler(OnModuleInitializationCompleted);
    }
    this._moduleStatusGrid.DataSource = this._moduleDataList.ToArray();

    this._footprint.Activate();

  }

  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 &gt;&gt;");
    this._moduleStatusGrid.DataSource = this._moduleDataList.ToArray();
    this._moduleStatusGrid.Invalidate();
    ModuleStatusWindow.Logger.LogMessage("CheckIfAllModulesInitComplete", LogMessageType.Information, "Updated grid control...");
    if (this._moduleDataList.Count(moduleData =&gt; !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 &lt;&lt;");
  }

  public void OnModuleInitializationStarted(object sender, EventArgs e)
  {
    ModuleStatusWindow.Logger.LogMessage("OnModuleInitializationStarted", LogMessageType.Information, "Received {0}", (sender as IModule).Name);
    if (this.InvokeRequired)
    {
      this.BeginInvoke(new ECEventsHandler(OnModuleInitializationStarted), sender, e);
    }
    else
    {
      ModuleStatusWindow.Logger.LogMessage("OnModuleInitializationStarted", LogMessageType.Information, "Updating the grid...");
      this._moduleStatusGrid.DataSource = this._moduleDataList.ToArray();
      this._moduleStatusGrid.Invalidate();
      ModuleStatusWindow.Logger.LogMessage("OnModuleInitializationStarted", LogMessageType.Information, "Updated module status grid.");
    }
  }

  private void ConnectToEquipmentControl(out IEquipmentControl equipMentControl)
  {
    ServerConfig equipmentConfig = ICECommonInterface.GetServerConfiguration("ICEEquipmentControl");

    TcpChannel clientChannel = null;
    equipMentControl = RemoteConnectionManager.RegisterClient&lt;IEquipmentControl&gt;(equipmentConfig, out clientChannel);       
  }

  private void InitializeFootPrintControl()
  {
    this._footprint.ImportConfiguration(ICECommonInterface.ConfigPath + @"\FootPrintConfig.xml");
    this._footprint.Initialize();
  }
}
</code></pre>
