별도의 스레드에서 강력한 방법 dowork을 호출하고 3 초 이상 걸리는 경우이를 죽이고 싶습니다. 다음 코드에 문제가 있습니까?멀티 스레딩 질문
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("starting new thread");
Thread t = new Thread(new ThreadStart(dowork));
t.Start();
DateTime start = DateTime.Now;
TimeSpan span = DateTime.Now.Subtract(start);
bool wait = true;
while (wait == true)
{
if (span.Seconds > 3)
{
t.Abort();
wait = false;
}
span = DateTime.Now.Subtract(start);
}
Console.WriteLine("ending new thread after seconds = {0}", span.Seconds);
Console.WriteLine("all done");
Console.ReadLine();
}
static void dowork()
{
Console.WriteLine("doing heavy work inside hello");
Thread.Sleep(7000);
Console.WriteLine("*** finished**** doing heavy work inside hello");
}
}
제한 시간으로 참여를 사용하는 것을 잊어 버렸습니다. 이것은 작업이 그보다 더 빨리 끝나면 3 초 이내에 작업을 계속하기 때문에 나의'Sleep'보다 낫습니다. –