c# - Thread.Sleep 和 Task.Delay 随机挂起且永不结束
问题描述
不是重复的。下面的代码也挂起并导致控制台“随机”等待输入 - 某个地方存在更大的潜在问题。
int i = 0;
while (true)
{
Console.WriteLine(++i);
System.Threading.Thread.Sleep(3000);
}
所以我有最奇怪的错误,网上似乎没有这个。我最近将我的任务调度程序移到了另一台服务器(Windows server 2016 标准),并且由于移动,应用程序似乎随机挂在 Task.Delay 行上,并且永远不会执行超过它。
例如,它会简单地说“检查在 09:31 完成下一次运行”,然后不再执行。
有趣的是,按 Enter 进入控制台窗口似乎会触发它执行,但是每次它都会挂在睡眠状态,直到应用程序关闭并重新打开。
它可以持续很多天而没有遇到这个问题,然后它似乎会随机出现。
我尝试使用 Thread.Sleep 而不是 Task.Delay 并且都遇到了同样的问题。我还添加了一个检查,以查看 Directory.Exists 命令是否可能导致它挂起,因此将目录存储在内存中。
我不知道是什么原因造成的——我以前从未经历过这样的事情。有任何想法吗?
代码如下 - 但是由于错误的奇怪性质,无法复制 id 想象
TLDR: Windows Server 2016 控制台应用程序在 Task.Delay(和 Thread.Sleep)上挂起,直到在控制台窗口上按下按键
// This is the main execution loop
while (true)
{
Task run = checkDirectoriesForAppsToRun();
run.Wait();
}
static async Task checkDirectoriesForAppsToRun()
{
Console.WriteLine("Starting apps...");
foreach (string subFolder in listOfSubfolders)
{
if (directoryExists.ContainsKey(currentDirectory + @"\" + subFolder) || System.IO.Directory.Exists(currentDirectory + @"\" + subFolder)) // Only if the sub directory exists!
{
if (directoryExists.ContainsKey(currentDirectory + @"\" + subFolder) == false)
directoryExists.Add(currentDirectory + @"\" + subFolder, true);
foreach (string directory in System.IO.Directory.GetDirectories(currentDirectory + @"\" + subFolder))
{
CheckDirectoryForApp(directory); // Load the apps xml file, and check if we need to run it.
}
}
else
{
Console.WriteLine(subFolder + " Doesn't exist in the root directory - Please check");
}
}
Console.WriteLine("Check completed next run at " + DateTime.Now.Add(timeToPause).ToShortTimeString());
await Task.Delay(timeToPause);
}
解决方案
因此,这似乎是 RDP 连接在运行时冻结 .NET 应用程序的问题。
不是 Windows Server 2008 上的问题 - 是更高版本的 Windows Server 上的问题。请参阅下面的链接。目前无法解决 - 摆脱 RDP 以支持 VNC 以回避问题
推荐阅读
- javascript - 如何在贝宝结帐中添加更多金额
- javascript - 如何从firestore获取没有元数据的文档
- python - 如何将列表的元组转换为仅元组?
- arrays - Perl:匿名列表的长度
- sql - 如何在 Firestore 数据库中生成 SQL WHERE IN 或 = ANY() 条件
- apache-superset - Apache Superset:sqllab - 结果后端未配置
- ansible - 如何在ansible中扩展dict,而不是覆盖它?
- python - 为什么在`id(i) is id(j)`中“is”运算符的行为与预期不同?
- android - Android Studio 无法链接到 -l1
- angular - Angular 7:setTimeout 方法在选择标签中不起作用