thread.interrupt only working once
-
nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using
Thread.Sleep(Timeout.Infinite);
to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpretedprivate static Thread TimeoutThread;
private void Form1Load(object sender, EventArgs e)
{
var Job = new ThreadStart(TestTimeout);
TimeoutThread = new Thread(Job) { IsBackground = true };
TimeoutThread.Start();
}private static void TestTimeout() { MessageBox.Show("Run"); try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { TestTimeout(); } } private void button1\_Click(object sender, EventArgs e) { TimeoutThread.Interrupt(); }
Can anyone tell me how to resolve this issue
-
nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using
Thread.Sleep(Timeout.Infinite);
to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpretedprivate static Thread TimeoutThread;
private void Form1Load(object sender, EventArgs e)
{
var Job = new ThreadStart(TestTimeout);
TimeoutThread = new Thread(Job) { IsBackground = true };
TimeoutThread.Start();
}private static void TestTimeout() { MessageBox.Show("Run"); try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { TestTimeout(); } } private void button1\_Click(object sender, EventArgs e) { TimeoutThread.Interrupt(); }
Can anyone tell me how to resolve this issue
I think that recursive call to TestTimeout is causing some problems. Does something like this do what you want (I have added a spinwait to simulate some work being done).
private static Thread TimeoutThread; private void Form1\_Load(object sender, EventArgs e) { ThreadStart Job = new ThreadStart(TestTimeout); TimeoutThread = new Thread(Job) { IsBackground = true }; TimeoutThread.Start(); } private static void TestTimeout() { while (true) { MessageBox.Show("Starting Work"); try { Thread.SpinWait(100000000); MessageBox.Show("Finished Work"); Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { } } } private void button1\_Click(object sender, EventArgs e) { TimeoutThread.Interrupt(); }
-
nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using
Thread.Sleep(Timeout.Infinite);
to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpretedprivate static Thread TimeoutThread;
private void Form1Load(object sender, EventArgs e)
{
var Job = new ThreadStart(TestTimeout);
TimeoutThread = new Thread(Job) { IsBackground = true };
TimeoutThread.Start();
}private static void TestTimeout() { MessageBox.Show("Run"); try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { TestTimeout(); } } private void button1\_Click(object sender, EventArgs e) { TimeoutThread.Interrupt(); }
Can anyone tell me how to resolve this issue
check out once that may be your form_load event called many times..