473,756 Members | 3,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer Array in c#, VS2005

I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.

Problem is that the system timers do not have a tag property so I can
tie in an object.

example (old way):

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(in t NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.T imer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = new System.Timers.T imer();
newTimer.Elapse d += timerWatch_Tick ;
newTimer.Interv al = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enable d = true;

arrTimers.Add(n ewTimer);
}
}

private void timerWatch_Tick (object sender, EventArgs e)
{
string sPath = ((TimerInfo)((T imer)sender).Ta g).sPath;
CheckForFile(sP ath);
}
Do I need to call Dispose if I want to reset the timer array?

foreach (System.Timers. Timer atimer in arrTimers)
{
atimer.Dispose( );
}
arrTimers.Clear ();

Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.

~Gina~

Oct 4 '06 #1
12 5528
There are directory watchers just for this purpose. You can tell it to
watch certain files, wildcards, and directories. You then get callbacks
when something happens.

Lookup File System Watcher in your help.
"Gina_Maran o" <gi*******@gmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
>I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.

Problem is that the system timers do not have a tag property so I can
tie in an object.

example (old way):

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(in t NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.T imer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = new System.Timers.T imer();
newTimer.Elapse d += timerWatch_Tick ;
newTimer.Interv al = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enable d = true;

arrTimers.Add(n ewTimer);
}
}

private void timerWatch_Tick (object sender, EventArgs e)
{
string sPath = ((TimerInfo)((T imer)sender).Ta g).sPath;
CheckForFile(sP ath);
}
Do I need to call Dispose if I want to reset the timer array?

foreach (System.Timers. Timer atimer in arrTimers)
{
atimer.Dispose( );
}
arrTimers.Clear ();

Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.

~Gina~

Oct 4 '06 #2
I have had nothing except bad luck with those directory watchers. One
reason that might be the reason is that I am watching network
directories and I doubt I will catch those events.

thanks though

~Gina~

On Oct 4, 4:33 pm, "EmeraldShi eld" <emeraldshi...@ noemail.noemail >
wrote:
There are directory watchers just for this purpose. You can tell it to
watch certain files, wildcards, and directories. You then get callbacks
when something happens.

Lookup File System Watcher in your help.

"Gina_Maran o" <ginals...@gmai l.comwrote in messagenews:11* *************** ******@h48g2000 cwc.googlegroup s.com...
I have created an array of timers (1-n). At first I just created
windows form timers but I read that system timers are better for
background work. The timers will just be monitoring different
directories and updating a database. No interaction with the GUI.
Problem is that the system timers do not have a tag property so I can
tie in an object.
example (old way):
public class TimerInfo
{
public string sPath;
public int iInterval;
}
private ArrayList arrTimers = new ArrayList();
private void CreateTimers(in t NUMBER_TIMERS)
{
TimerInfo Info;
System.Timers.T imer newTimer;
for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;
newTimer = new System.Timers.T imer();
newTimer.Elapse d += timerWatch_Tick ;
newTimer.Interv al = 20000;
newTimer.Tag = TimerInfo;
newTimer.Enable d = true;
arrTimers.Add(n ewTimer);
}
}
private void timerWatch_Tick (object sender, EventArgs e)
{
string sPath = ((TimerInfo)((T imer)sender).Ta g).sPath;
CheckForFile(sP ath);
}
Do I need to call Dispose if I want to reset the timer array?
foreach (System.Timers. Timer atimer in arrTimers)
{
atimer.Dispose( );
}
arrTimers.Clear ();
Thanks for your help. I am struggling as a newbie through C# even
though it is a really neat language.
~Gina~- Hide quoted text -- Show quoted text -
Oct 4 '06 #3
I better clearly state my main question (besides maybe a code review)
is:

Is there a trick so I can assign an object to a system timer since it
does not have a tag field?

example (old way using form timer) but I need to

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(in t NUMBER_TIMERS)
{
TimerInfo Info;
Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = Timers.Timer();
newTimer.Tick += timerWatch_Tick ;
newTimer.Interv al = 20000;
newTimer.Tag = TimerInfo; //<-- this does not exist for the
system timer
newTimer.Enable d = true;

arrTimers.Add(n ewTimer);
}
}

private void timerWatch_Tick (object sender, EventArgs e)
{
(TimerInfo)((Ti mer)sender).Ena bled = false;
try
{
string sPath = ((TimerInfo)((T imer)sender).Ta g).sPath;
CheckForFile(sP ath);
}
finally
{
(TimerInfo)((Ti mer)sender).Ena bled = true;
}
}

~Gina~

Oct 5 '06 #4
Are you saying you want a timer object that does not have a tag info? Or a
object that has everything a timer has and also has a tag object?


"Gina_Maran o" <gi*******@gmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
>I better clearly state my main question (besides maybe a code review)
is:

Is there a trick so I can assign an object to a system timer since it
does not have a tag field?

example (old way using form timer) but I need to

public class TimerInfo
{
public string sPath;
public int iInterval;
}

private ArrayList arrTimers = new ArrayList();

private void CreateTimers(in t NUMBER_TIMERS)
{
TimerInfo Info;
Timer newTimer;

for (int i = 1; i <= NUMBER_TIMERS; i++)
{
Info = new TimerInfo();
Info.sPath = "c:\\";
Info.iInterval = 60000;

newTimer = Timers.Timer();
newTimer.Tick += timerWatch_Tick ;
newTimer.Interv al = 20000;
newTimer.Tag = TimerInfo; //<-- this does not exist for the
system timer
newTimer.Enable d = true;

arrTimers.Add(n ewTimer);
}
}

private void timerWatch_Tick (object sender, EventArgs e)
{
(TimerInfo)((Ti mer)sender).Ena bled = false;
try
{
string sPath = ((TimerInfo)((T imer)sender).Ta g).sPath;
CheckForFile(sP ath);
}
finally
{
(TimerInfo)((Ti mer)sender).Ena bled = true;
}
}

~Gina~

Oct 5 '06 #5
Hi Daniel:

I currently have an arraylist of System.Timers.T imer Timers.

The System.Timers.T imer Timers do not have a Tag property.I use the tag
property of the Timer to store an object so that on the tick/elapse
event I can get timer specific settings.

I am open to any ideas. I read that System.Timers.T imer Timers are the
timers to use since it does not interact with the GUI at all.

Please ask as many questions as you wish. I am new to C# and probably
don't always use the correct terminology.

~Gina~

Daniel wrote:
Are you saying you want a timer object that does not have a tag info? Or a
object that has everything a timer has and also has a tag object?

"Gina_Maran o" <gi*******@gmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
I better clearly state my main question (besides maybe a code review)
is:

Is there a trick so I can assign an object to a system timer since it
does not have a tag field?
Oct 5 '06 #6
Hi Gina

"The System.Timers.T imer Timers do not have a Tag property"

Yes they do have a tag property, i just checked and a timer has the tag.

I get what yiu are doing as i do something similar in my app. You have a
load of timers in an array and you want to know when a particular timer has
finished or its time has elapsed?

As i said they do have a tag property so i am confused about you saying that
or why you dont think they have one?

In my situation i did this:

Make a TimerManager class
Make a MyTimer class (or some better app specific name)

Now in your MyTimer class do this

class MyTimer : Timer //and inherit from timer

Now you can add any attributes you like to your timer and it will have all
the functionality of the normal timer.

so if you have an enumerator such as (just using examples of timer
categories)

enum TimerType : int
{
LunchBreak,
ClassLength
}

You could then do this in MyTimer class

class MyTimer : Timer
{
TimerType _myTimerType;

public event timerElapsed;
public delegate timerElapsed TimeElapsed(Tim erType t);

MyTimer(TimerTy pe t)
{
_myTimerType = t;
Elapsed += OnTimerElapsed; //.net 2 syntax
}

public TimerType
{
get{return _myTimerType; }
}

public void OnTimerElapsed( object sender,
System.Timers.E lapsedEventArgs e)
{
timerElapsed(_m yTimerType);
}

}
Then in your TimerManager class do this

class TimerManager
{
private ArrayList MyTimerList;
TimerManager(){ };

//create timers
public void CreateTimer(Tim erType t, int interval);
{
MyTimer m = new MyTimer(t);
m.Interval = interval;
m.timerElapsed += TimerComplete;
MyTimerList.Add (m);
}

public void TimerComplete(T imerType t)
{
switch(t)
{
case LunchBreak:
//do something
break;

case ClassLength:
//do something
break;
}
}

}
I have written that off the top of my head so i dont know if it will compile
but i hope it gives you an idea of one implementation method? By doing that
you wrap up the timer to have functionality you need and only ever need the
TimerManager. So to create a timer from some outside class:

TimerManager tm = new TimerManager();
tm.CreateTimer( TimerType.Lunch Break);

Of course you need to add methods to the timer manager class for starting
the timers, or have them start the moment they are created and similarly the
same for stopping them, just a foreach to iterate through the array would do
it.

So the idea is the enumerator makes each timer you create unique. You can
easily adjust to pass more data from the MyTimer into the TimerComplete and
this allows a central manegemt of all the timers. So the TimerComplete in
the case above will fire everytime any timer elapsed its interval and then
it checks which timer it was and you can react as you see fit.

Thats the way i did what i think you are trying to do. Does that help at
all?



"Gina_Maran o" <gi*******@gmai l.comwrote in message
news:11******** *************@m 7g2000cwm.googl egroups.com...
Hi Daniel:

I currently have an arraylist of System.Timers.T imer Timers.

The System.Timers.T imer Timers do not have a Tag property.I use the tag
property of the Timer to store an object so that on the tick/elapse
event I can get timer specific settings.

I am open to any ideas. I read that System.Timers.T imer Timers are the
timers to use since it does not interact with the GUI at all.

Please ask as many questions as you wish. I am new to C# and probably
don't always use the correct terminology.

~Gina~

Daniel wrote:
>Are you saying you want a timer object that does not have a tag info? Or
a
object that has everything a timer has and also has a tag object?

"Gina_Marano " <gi*******@gmai l.comwrote in message
news:11******* *************** @m73g2000cwd.go oglegroups.com. ..
>I better clearly state my main question (besides maybe a code review)
is:

Is there a trick so I can assign an object to a system timer since it
does not have a tag field?

Oct 5 '06 #7
Hi Daniel:

I really appreciate the effort of your response. It looks like it will
give me what I want which is the ability to assign each individual
timer a path or other properties that the timer will reference on its
elapse event.

As for the tag property. I think you may be refering to a different
timer that I am referring to. there is a windows.form.ti mer (or
something close) and a System.Timers.T imer which is used for services
or in my case, a form without the need for the timer to interact with
the GUI.

http://www.informit.com/guides/conte...eqNum=200&rl=1
(The site seems really slow today, you may need to keep refreshing
until the article actually pops up)

System.Timers.T imer:

Public Constructors
Timer
Public Properties
AutoReset
Container
Enabled
Interval
Site
SynchronizingOb ject
Protected Properties
CanRaiseEvents
DesignMode
Events
Public Methods
BeginInit
Close
CreateObjRef
Dispose
EndInit
Equals
GetHashCode
GetLifetimeServ ice
GetType
ReferenceEquals
Start
Stop
ToString

But what I gave me may be what I wanted anyway. Using the tag was a
hack to get what I wanted.

~Gina~

Oct 5 '06 #8
But you can pass a state object to the constructor that gets passed to the
callback to get the same effect - no?

--
William Stacey [C# MVP]

"Gina_Maran o" <gi*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
| Hi Daniel:
|
| I really appreciate the effort of your response. It looks like it will
| give me what I want which is the ability to assign each individual
| timer a path or other properties that the timer will reference on its
| elapse event.
|
| As for the tag property. I think you may be refering to a different
| timer that I am referring to. there is a windows.form.ti mer (or
| something close) and a System.Timers.T imer which is used for services
| or in my case, a form without the need for the timer to interact with
| the GUI.
|
| http://www.informit.com/guides/conte...eqNum=200&rl=1
| (The site seems really slow today, you may need to keep refreshing
| until the article actually pops up)
|
| System.Timers.T imer:
|
| Public Constructors
| Timer
| Public Properties
| AutoReset
| Container
| Enabled
| Interval
| Site
| SynchronizingOb ject
| Protected Properties
| CanRaiseEvents
| DesignMode
| Events
| Public Methods
| BeginInit
| Close
| CreateObjRef
| Dispose
| EndInit
| Equals
| GetHashCode
| GetLifetimeServ ice
| GetType
| ReferenceEquals
| Start
| Stop
| ToString
|
| But what I gave me may be what I wanted anyway. Using the tag was a
| hack to get what I wanted.
|
| ~Gina~
|
Oct 5 '06 #9
As an alternative you could derive from the timer class as below. That
way you can add any additional information you require.

public class Class1 : System.Timers.T imer
{
string _name;

public Class1(string pName)
{
_name = pName;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}

Oct 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3298
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer objects which all fire into my OnTimer( object state ) function very nicely. I pass in an object "o" on creation of this timer which I subsequently get passed to me in my OnTimer function. Now in the OnTimer function I want to modify the object...
1
3956
by: Manuel | last post by:
Used VS2005 to create a windows service and I can't make a timer trigger the tick/elapsed event. On the form viewer I dragged a timer from the components section of the toolbar, enabled it but the tick event does not fire! I tried this other method but the elapsed event does not fire. ---------- Private WithEvents MyTmr As New System.Timers.Timer(1000) Private Sub MyTmr_Elapsed(ByVal sender As Object, ByVal e As
5
2513
by: Lonewolf | last post by:
Hi all, I am not sure if it is even possible to do it from .NET itself. Is there something like a waitable timer which can resume the system from its hibernate or standby (S3: suspend to ram) mode. I'm trying to use C# to create something that can "wake" the system up at a particular time (set by user) if it is in hibernate or standby mode to do certain task and after which to put it back to sleep again. Is it possible? I'm using VS2005....
3
2913
by: Avi G | last post by:
Hi, how can i set a timer in VS2005 form that will click the button that is on the form every X time? THX
0
897
by: John Mason | last post by:
Hello, I have a small client app using a timer control that communicates with a Windows service using .NET remoting. Both apps were authored against the 1.1.4322 framework. The timer controls the periodicity for refreshing the details/progress of the Windows service. This all works perfectly in my test environment running 1.1. The production environment runs off the 2.0 framework. The Windows service (which uses no timers) runs fine,...
4
1509
by: John | last post by:
I want to write a quick app that will remind me not to slouch at the computer with an Outlook "New Mail Desktop Alert" style pop-up every 15 minutes or so. What I'd like advice on is the best method of timing and displaying the pop-ups that would have the lowest performance impact for other running applications? Best regards
5
12247
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
19
5209
by: colin | last post by:
Hi, Im trying to time some parts of my code using the Stopwatch class, but it is so slow, I tried the QueryPerformanceFrequency() but this seems to be just as slow, if I just simply call this function in a loop it takes 21 ticks with a reported frequency of 3.5mhz its is about 6us wich is over 12k instructions on my 2ghz cpu. this negates the idea of having a high res timer ...
5
3794
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
9325
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9152
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9716
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4996
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.