Thread Problems
-
I have an application that will be running in the background and I want it to run every 5 minutes to check a file execute it and then wait another 5 minutes to do this. I tried doing this, but I get the following error.
Unhandled Exception: System.Threading.THreadStateException: Thread is running or terminated; it ca not restart.
Thread t1 = new Thread( new ThreadStart(myMethod)); bool start = true; int stop = 0; while(start) { t1.Start(); Thread.sleep(5000); if(stop==1000) { start = false; } stop++; }
-
I have an application that will be running in the background and I want it to run every 5 minutes to check a file execute it and then wait another 5 minutes to do this. I tried doing this, but I get the following error.
Unhandled Exception: System.Threading.THreadStateException: Thread is running or terminated; it ca not restart.
Thread t1 = new Thread( new ThreadStart(myMethod)); bool start = true; int stop = 0; while(start) { t1.Start(); Thread.sleep(5000); if(stop==1000) { start = false; } stop++; }
You don't want the application controlling the thread sleeping, you want the thread itself sleeping and determining when to exit.
void MakeThread()
{
Thread t1 = new Thread( new ThreadStart(MyMethod));
t1.IsBackground=true;
t1.Start();
}void MyMethod()
{
while (++stop != 1000)
{
// Do stuff
Thread.Sleep(5000);
}
}Note setting the IsBackground to true, so if your application exits, the thread will terminate too. Otherwise you can have threads running even when your application has quit. However, this isn't really what threads are intended for. Sounds more like what you want is a timer that fires every 5 seconds. Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith -
I have an application that will be running in the background and I want it to run every 5 minutes to check a file execute it and then wait another 5 minutes to do this. I tried doing this, but I get the following error.
Unhandled Exception: System.Threading.THreadStateException: Thread is running or terminated; it ca not restart.
Thread t1 = new Thread( new ThreadStart(myMethod)); bool start = true; int stop = 0; while(start) { t1.Start(); Thread.sleep(5000); if(stop==1000) { start = false; } stop++; }
-
I have an application that will be running in the background and I want it to run every 5 minutes to check a file execute it and then wait another 5 minutes to do this. I tried doing this, but I get the following error.
Unhandled Exception: System.Threading.THreadStateException: Thread is running or terminated; it ca not restart.
Thread t1 = new Thread( new ThreadStart(myMethod)); bool start = true; int stop = 0; while(start) { t1.Start(); Thread.sleep(5000); if(stop==1000) { start = false; } stop++; }
I concur with the previous remarks, but want to add you constructed a loop that really deserved a for statement: for (int i=0; i<1000; i++) { // do here what has to be done periodically // ... // now wait Thread.Sleep(5000); } This approach repeats an operation with (an attempt to) a fixed time lapse in between; if you want (an attempt to) predictable starting times, you definitely need to use a timer. :)
Luc Pattyn
-
I concur with the previous remarks, but want to add you constructed a loop that really deserved a for statement: for (int i=0; i<1000; i++) { // do here what has to be done periodically // ... // now wait Thread.Sleep(5000); } This approach repeats an operation with (an attempt to) a fixed time lapse in between; if you want (an attempt to) predictable starting times, you definitely need to use a timer. :)
Luc Pattyn
I thought using thread will make the methods to run every 5 minutes. So basicly i want the application to run in the background, I don't want to open the WindowsConsole and to exit the program I want to go to the task manager to stop it. So how can I make this that every 5 minutes it executes MyMethod():
-
I thought using thread will make the methods to run every 5 minutes. So basicly i want the application to run in the background, I don't want to open the WindowsConsole and to exit the program I want to go to the task manager to stop it. So how can I make this that every 5 minutes it executes MyMethod():
You can make an app that does not show its main form, but shows a NotifyIcon. Its main task would be to either hold a timer or a wait loop (as I have shown), so whatever needs to be done gets done periodically. You could add a ContextMenu with an "Exit" menu item that stops it (no need to kill it with Task Manager!), and possibly some other menu items that allow you to see some status, or set some parameters. I guess there must be several examples available on CodeProject. :)
Luc Pattyn
-
You can make an app that does not show its main form, but shows a NotifyIcon. Its main task would be to either hold a timer or a wait loop (as I have shown), so whatever needs to be done gets done periodically. You could add a ContextMenu with an "Exit" menu item that stops it (no need to kill it with Task Manager!), and possibly some other menu items that allow you to see some status, or set some parameters. I guess there must be several examples available on CodeProject. :)
Luc Pattyn