Why does this code behave different on Windows and Linux (using Mono)?
static void Main(string[] args)
{
Stopwatch stopwatch = Stopwatch.StartNew();
Task[] tasks = new Task[1];
tasks[0] = Task.Run(() =>
{
IPHostEntry iphe = Dns.GetHostEntry("8.8.8.8.dnsrbl.org");
});
Task.WaitAll(tasks, 2000);
Console.WriteLine("Done in " + stopwatch.ElapsedMilliseconds + " ms");
}
8.8.8.8.dnsrbl.ru is an example of a query that will eventually time out. I believe there is no working DNS server (or its firewall is blocking me).
Anyway, the point is not getting a result from the DNS server, the point is how Task.WaitAll() behaves on Windows and Mono when waiting for a task containing a call to Dns.GetHostEntry().
On Windows the program takes more or less 2 seconds to run when the query is not returning any result within the timeout period (2s). That is, Task.WaitAll with timeout seems to work. Running this program on Linux with Mono takes 2 seconds to get the output, but the program does not terminate until the task exits. Why is that?
It seems like I get the same execution time regardless if I use Time.WaitAll with timeout or not.
The clue is in Dns.GetHostEntry() because Task.WaitAll() works as expected if I start a task with a Thread.Sleep() simulating a long running task.
This works as expected:
tasks[0] = Task.Run(() => Thread.Sleep(10000));
Is there a way to force Task.WaitAll(Task[] tasks, int millisecondsTimeout) to actually time out when running in Mono?
Edit: Task.WaitAll() does in fact return after the timeout period, but the program doesn't terminate when running in Mono (until Dns.GetHostEntry times out).
And it's not the compiler. I get the same result whether I compile with Visual Studio or the Mono C# compiler.