473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

C# thread and Timer

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
5 18889
Plater
7,872 Expert 4TB
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
Kate77
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
7,872 Expert 4TB
Worked fine for me ?
I can't even find a way to get that error.
Oct 30 '07 #4
Kate77
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
7,872 Expert 4TB
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

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

Similar topics

6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the...
12
by: Crirus | last post by:
Hi! I havea control that display messages... I added a timer to it Every time a noew mesaage should be displayed I do the following: Private sub ShowMsg(message as string) Me.lblStatus.Text...
4
by: Rich P | last post by:
Greetings, I have a routine I was running in VB6 on a timed schedule. When the timeframe came up, the timer would be disable, the routine would run, and the timer gets enabled. I am trying to...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
1
by: Ty | last post by:
I am developing a .net application using C# that requires a 1ms timer. I have tried creating said timer by creating a timer object (System.Timers.Timer) that is seeded with a 1ms interval. I have...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
4
by: liu yang | last post by:
I want to build a timer for each thread, and the timer must be thread- safe. How to do that? My environment is Linux and Pthread. Thanks.
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.