C# thread and Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kate77
    New Member
    • Oct 2007
    • 14

    C# thread and Timer

    Hello,

    Im trying to do something very simple but having problems..
    What I want to accomplish sounds simple but I cant find anywhere on the web answer of how to do that so it will work.

    I want to run a thread , for example map the files on the hard drive.
    now what I want to do is run a timer that will check the thread state so when it is finish the timer will run a second thread that will map the files on the hard drive again but with different params this time.

    the problem I am facing is that after i do thread.start if i define a new timer that will check the function of the timer cant find the thread.

    here is example code just to illustrate:

    public string Scan(string id)
    {

    // Create the thread object
    // via a ThreadStart delegate. This does not start the thread.
    Thread z = new Thread(delegate () { SearchDrive("*. exe"); });
    //Start the thread
    z.Start();

    //here I want to start a timer that will check the thread state
    //so i do it as MS say
    system.Timers.T imer aTimer = new System.Timers.T imer();

    // Hook up the Elapsed event for the timer.
    aTimer.Elapsed += new ElapsedEventHan dler(OnTimedEve nt);

    // Set the Interval to 2 seconds (2000 milliseconds).
    aTimer.Interval = 2000;
    aTimer.Enabled = true;

    }


    // Specify what you want to happen when the Elapsed event is
    // raised.
    private static void OnTimedEvent(ob ject source, ElapsedEventArg s e)
    {

    //How can I make a z.threadstate check her ?

    I want every 2 seconds to check if thread is still active.
    and if it is finished running I want to run
    Thread x = new Thread(delegate () { SearchDrive("*. dll"); });
    //Start the thread
    x.Start();
    }



    I thought maybe fighting with threads which i dont know well,
    maybe i should add to the functions i run in threads to write to a
    status file when they finish running and then simply add regular timer that checks
    the status and runs the next function.

    any help will be appreciated,
    Thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Make Thread be defined in a way that it's available elsewhere.

    You are defining it in the function and then losing the handle. If you define outsite the function first, you can access it later.

    Code:
    private Thread z;
    
    public string Scan(string id)
    {
    
    // Create the thread object
    // via a ThreadStart delegate. This does not start the thread.
       z = new Thread(delegate() { SearchDrive("*.exe"); }); 
    //Start the thread
       z.Start();
    
    //here I want to start a timer that will check the thread state
    //so i do it as MS say
       System.Timers.Timer aTimer = new System.Timers.Timer();
    
    // Hook up the Elapsed event for the timer.
       aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    
    // Set the Interval to 2 seconds (2000 milliseconds).
       aTimer.Interval = 2000;
       aTimer.Enabled = true;
    
    }
    
    
    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
       if (z.ThreadState=ThreadState.Stopped)//or whatever it's supposed to be
       {
          //do something when thread is done.
          //z = new Thread(delegate() { SearchDrive("*.dll"); }); 
          //Start the thread
          //z.Start();
       }
    }

    Comment

    • Kate77
      New Member
      • Oct 2007
      • 14

      #3
      Thanks for the reply,

      but it still doesnt solve the problem.
      if I try to declare Thread z earlier I get the following error:
      Error 1 An object reference is required for the nonstatic field, method, or property



      Originally posted by Plater
      Make Thread be defined in a way that it's available elsewhere.

      You are defining it in the function and then losing the handle. If you define outsite the function first, you can access it later.

      Code:
      private Thread z;
      
      public string Scan(string id)
      {
      
      // Create the thread object
      // via a ThreadStart delegate. This does not start the thread.
         z = new Thread(delegate() { SearchDrive("*.exe"); }); 
      //Start the thread
         z.Start();
      
      //here I want to start a timer that will check the thread state
      //so i do it as MS say
         System.Timers.Timer aTimer = new System.Timers.Timer();
      
      // Hook up the Elapsed event for the timer.
         aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
      
      // Set the Interval to 2 seconds (2000 milliseconds).
         aTimer.Interval = 2000;
         aTimer.Enabled = true;
      
      }
      
      
      // Specify what you want to happen when the Elapsed event is 
      // raised.
      private static void OnTimedEvent(object source, ElapsedEventArgs e)
      {
         if (z.ThreadState=ThreadState.Stopped)//or whatever it's supposed to be
         {
            //do something when thread is done.
            //z = new Thread(delegate() { SearchDrive("*.dll"); }); 
            //Start the thread
            //z.Start();
         }
      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Worked fine for me ?
        I can't even find a way to get that error.

        Comment

        • Kate77
          New Member
          • Oct 2007
          • 14

          #5
          Originally posted by Plater
          Worked fine for me ?
          I can't even find a way to get that error.
          Thanks!

          The problem was in how i defined it,
          i did
          private Thread z;

          but

          private static Thread z;

          solved it.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Ahh, ok. I was wondering, because that error is generally a "add the static keyword" error.
            But your function wasn't listed as static so I was confused.

            Comment

            Working...