我的应用程序中有一个错误,似乎只有当我在调试器中暂停应用程序几分钟时才会显示它的面孔。我怀疑这是由于我使用的第三方网络库有一个心跳线程,当它的心跳线程暂停时它无法 ping 服务器时,它会断开连接。
我正在尝试为此编写一个测试用例应用程序以验证这是导致错误的原因。为此,我需要一种方法来暂停应用程序中的所有线程(稍后我将缩小到仅暂停我怀疑可能是心跳线程的线程)来模拟在调试器中暂停应用程序。
有谁知道如何做到这一点?一个线程甚至有可能导致另一个线程休眠吗?
谢谢,亚历克斯
更新:
我最终决定我并不真的需要一个应用程序来为我做这件事,因为这只是为了验证调试器中的暂停是否会导致断开连接。所以,这就是我所做的......(最简单的方法通常是最好的......或者至少是最简单的......)
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!