By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,484 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,484 IT Pros & Developers. It's quick & easy.

C# thread and Timer

P: 14
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.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)
{

//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.
Oct 30 '07 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
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.

Expand|Select|Wrap|Line Numbers
  1. private Thread z;
  2.  
  3. public string Scan(string id)
  4. {
  5.  
  6. // Create the thread object
  7. // via a ThreadStart delegate. This does not start the thread.
  8.    z = new Thread(delegate() { SearchDrive("*.exe"); }); 
  9. //Start the thread
  10.    z.Start();
  11.  
  12. //here I want to start a timer that will check the thread state
  13. //so i do it as MS say
  14.    System.Timers.Timer aTimer = new System.Timers.Timer();
  15.  
  16. // Hook up the Elapsed event for the timer.
  17.    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  18.  
  19. // Set the Interval to 2 seconds (2000 milliseconds).
  20.    aTimer.Interval = 2000;
  21.    aTimer.Enabled = true;
  22.  
  23. }
  24.  
  25.  
  26. // Specify what you want to happen when the Elapsed event is 
  27. // raised.
  28. private static void OnTimedEvent(object source, ElapsedEventArgs e)
  29. {
  30.    if (z.ThreadState=ThreadState.Stopped)//or whatever it's supposed to be
  31.    {
  32.       //do something when thread is done.
  33.       //z = new Thread(delegate() { SearchDrive("*.dll"); }); 
  34.       //Start the thread
  35.       //z.Start();
  36.    }
  37. }
  38.  
Oct 30 '07 #2

P: 14
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



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.

Expand|Select|Wrap|Line Numbers
  1. private Thread z;
  2.  
  3. public string Scan(string id)
  4. {
  5.  
  6. // Create the thread object
  7. // via a ThreadStart delegate. This does not start the thread.
  8.    z = new Thread(delegate() { SearchDrive("*.exe"); }); 
  9. //Start the thread
  10.    z.Start();
  11.  
  12. //here I want to start a timer that will check the thread state
  13. //so i do it as MS say
  14.    System.Timers.Timer aTimer = new System.Timers.Timer();
  15.  
  16. // Hook up the Elapsed event for the timer.
  17.    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  18.  
  19. // Set the Interval to 2 seconds (2000 milliseconds).
  20.    aTimer.Interval = 2000;
  21.    aTimer.Enabled = true;
  22.  
  23. }
  24.  
  25.  
  26. // Specify what you want to happen when the Elapsed event is 
  27. // raised.
  28. private static void OnTimedEvent(object source, ElapsedEventArgs e)
  29. {
  30.    if (z.ThreadState=ThreadState.Stopped)//or whatever it's supposed to be
  31.    {
  32.       //do something when thread is done.
  33.       //z = new Thread(delegate() { SearchDrive("*.dll"); }); 
  34.       //Start the thread
  35.       //z.Start();
  36.    }
  37. }
  38.  
Oct 30 '07 #3

Plater
Expert 5K+
P: 7,872
Worked fine for me ?
I can't even find a way to get that error.
Oct 30 '07 #4

P: 14
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.
Oct 30 '07 #5

Plater
Expert 5K+
P: 7,872
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.
Oct 30 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.