我的應用程序中有一個錯誤,只有當我在調試器中暫停應用程序幾分鐘時纔會顯示它的面部。我懷疑這是由於我使用的第三方網絡庫有一個心跳線程,當它在心跳線程暫停時無法ping服務器時,它會斷開連接。在運行時暫停當前進程中的所有線程
我想寫一個測試用例應用程序來驗證這是錯誤的原因。爲此,我需要一種方法來暫停應用程序中的所有線程(我將稍後縮小爲僅暫停我懷疑可能是心跳線程的線程)以模擬暫停調試器中的應用程序。
有誰知道如何做到這一點?一個線程甚至有可能導致另一個線程進入睡眠狀態?
謝謝, 亞歷克斯
UPDATE:
我最終決定,我並不真的需要一個應用程序爲我做到這一點,看到的一點是,只是爲了驗證,暫停在調試器正在導致斷開連接。所以,在這裏就是我所做的。(最簡單的方法往往是最好的......或者至少是最簡單的...)
private static void Main(string[] args)
{
IPubSubAdapter adapter = BuildAdapter();
bool waitingForMessage;
adapter.Subscribe(_topic, message => waitingForMessage = false, DestinationType.Topic);
Stopwatch timePaused = new Stopwatch();
while (adapter.IsConnected)
{
Console.WriteLine("Adapter is still connected");
waitingForMessage = true;
adapter.Publish(_topic, "testmessage", DestinationType.Topic);
while (waitingForMessage)
{
Thread.Sleep(100);
}
timePaused.Reset();
timePaused.Start();
Debugger.Break();
timePaused.Stop();
Console.WriteLine("Paused for " + timePaused.ElapsedMilliseconds + "ms.");
Thread.Sleep(5000); // Give it a chance to realise it's disconnected.
}
Console.WriteLine("Adapter is disconnected!");
Console.ReadLine();
}
和輸出:
Adapter is still connected
Paused for 10725ms.
Adapter is still connected
Paused for 13298ms.
Adapter is still connected
Paused for 32005ms.
Adapter is still connected
Paused for 59268ms.
Adapter is disconnected!
看起來很有希望,由於引發異常,我給它一個去 – AlexC 2010-11-10 12:58:05
隨時回到這裏,並告訴我們,如果它的工作。 :) – bitbonk 2010-11-10 13:04:12
您還需要GetCurrentThreadId(),這樣您纔不會暫停自己。隨機死鎖很可能,不要分配任何東西。 – 2010-11-10 13:19:25