Not a duplicate. The code directly below also hangs and causes the console to wait for input 'randomly' - Bigger underlying issue somewhere.
int i = 0;
while (true)
{
Console.WriteLine(++i);
System.Threading.Thread.Sleep(3000);
}
So i have the strangest bug and nothing online seems to ever have this. I recently moved my task scheduler to a different server (Windows server 2016 standard) and since the move the app seems to randomly hang on the Task.Delay line and never execute past it.
It will simply say 'Check completed next run at 09:31' for example, then never execute again.
Interestingly pressing enter into the console window seems to trigger it to execute, however then every time it hangs on the sleep until the application is closed and re-opened.
It can go many days without experiencing this issue, then it will seemingly randomly crop up.
I've tried with Thread.Sleep instead of Task.Delay and both experience the same issue. I also added a check to see if possibly the Directory.Exists command was causing it to hang, so stored the directories in memory instead.
I have no idea what could be causing this - I've never experienced anything like it before. Any ideas?
Code is below - however will be impossible to replicate id imagine due to the weird nature of the bug
TLDR: Windows server 2016 console application hangs on Task.Delay (and Thread.Sleep) until key push on console window
// 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);
}
So it seems this is an issue with RDP connectivity freezing .NET applications at runtime.
Was not an issue on Windows server 2008 - is an issue on later windows server versions. See link below. Currently been unable to resolve - Got rid of RDP in favour of VNC to sidestep the issue