Here's a code I made in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace xyz
{
class Program
{
Thread t1, t2;
static void Main(string[] args)
{
Program go = new Program();
go.actual();
}
public void actual()
{
t1 = new Thread(timer);
t2 = new Thread(inputting);
t1.Start();
t2.Start();
}
public void timer()
{
Thread.Sleep(5000);
t2.Abort();
Console.WriteLine("5 seconds passed!");
}
public void inputting()
{
Console.WriteLine("Please wait 5 seconds...");
Console.ReadKey();
}
}
}
Now, the problem is, when the console says "5 seconds passed!" (after aborting the t2
thread), it doesn't exit right away. This text stays there for a couple of seconds and then the console exits.
The thing is, if I press a key (of the ReadKey
method) before the thread aborts, it displays the "5 seconds passed!" text and then it exits right away.
If I don't click any key, and the ReadKey
method doesn't happen, it just displays the text for a couple of seconds.
Why is that? Is it a bug? And can I fix it?