2011-04-28 86 views
2

UI線程偶爾會在語句'if(this.InvokeRequired)'以下面的方法掛起。InvokeRequired掛起

你能幫我找出問題

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 <<"); 
    } 
+2

您是如何確定它是否在那裏懸掛*特別是*? – 2011-04-28 11:18:21

+0

從日誌文件中,我可以在此InvokeRequired語句之前看到日誌條目。但是CheckIfAllModulesInitComplete中的日誌條目不存在於日誌文件中 – Maanu 2011-04-28 11:26:12

+0

任何可能導致掛起的記錄器調用的實際機會? – BugFinder 2011-04-28 11:28:31

回答

0

更可能的原因,你有某種這是導致死鎖的競爭條件。或者,你的調試信息混亂了,這不是真正的阻塞線。

1

我不認爲InvokeRequired可能會掛起。 BeginInvoke可能但我不認爲它會。

想法很少。

BeginInvoke運行正常,但UI線程很忙,所以它永遠不會運行OnModuleInitializationComplete。這個線程繼續做什麼?它是否開始等待(如調用EndInvoke)作爲某個點?

InvokeRequired返回false,您的CheckIfAllModulesInitComplete方法掛起。

我會添加更多日誌記錄到OnModuleInitializationComplete,以顯示如果它已採取的路徑,然後用新信息更新您的問題。
如果您還可以提供有關此方法的更多細節的代碼,它可能會很有用,尤其是在任何需要等待此方法完成的地方。

+0

我在我的第一篇文章中添加了CheckIfAllModulesInitComplete的源代碼。該方法中的第一個日誌條目在日誌文件中不可用 – Maanu 2011-04-28 11:43:06

1

我會在InvokeRequired之後和BeginInvoke調用之前添加一條日誌消息。

我懷疑這是BeginInvoke阻塞,因爲UI線程很忙,也許是因爲它正在等待其他東西。