Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread Problems

Thread Problems

Scheduled Pinned Locked Moved C#
help
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eddymvp
    wrote on last edited by
    #1

    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++; }

    M G L 3 Replies Last reply
    0
    • E Eddymvp

      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++; }

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      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

      Thyme In The Country

      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

      1 Reply Last reply
      0
      • E Eddymvp

        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++; }

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You are restarting the thread every five seconds, not every five minutes. If the thread ever takes more than five seconds to complete, you will be trying to restart a thread that is still running.

        --- Year happy = new Year(2007);

        1 Reply Last reply
        0
        • E Eddymvp

          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++; }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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

          E 1 Reply Last reply
          0
          • L 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

            E Offline
            E Offline
            Eddymvp
            wrote on last edited by
            #5

            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():

            L 1 Reply Last reply
            0
            • E Eddymvp

              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():

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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

              E 1 Reply Last reply
              0
              • L 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

                E Offline
                E Offline
                Eddymvp
                wrote on last edited by
                #7

                thanks for the suggestion, I think is a good idea to have a ContextMenu and and a textarea to check the status.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups