我的应用程序中有一个错误,只有当我在调试器中暂停应用程序几分钟时才会显示它的面部。我怀疑这是由于我使用的第三方网络库有一个心跳线程,当它在心跳线程暂停时无法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