create Timers dynamically
-
Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance
-
Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance
Hmmm.. i have a feeling your going about this the wrong way... What are you trying to do with each timer? There is nothing in your example code that shows you doing anything with the instance of a timer that would need it to be identified
Life goes very fast. Tomorrow, today is already yesterday.
-
Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance
You can cast to a Timer from the sender
if(sender is System.Timers.Timer)
{
System.Timers.Timer thisTimer = sender as System.Timers.Timer;
}If you want you can give it a property such as an int for ID or a string for Name etc by subclassing e.g.
public class MyTimer : System.Timers.Timer
{
public string Name
{
get;
set;
}
}... and then use
MyTimer
instead ofTimer
.Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hmmm.. i have a feeling your going about this the wrong way... What are you trying to do with each timer? There is nothing in your example code that shows you doing anything with the instance of a timer that would need it to be identified
Life goes very fast. Tomorrow, today is already yesterday.
Hi Thanks a lot for reoplying so soon I cant tell the requirement completely. Let me explain in brief.In database I have a set of records in this structure. ID ConsumerID Time Interval 1 5 5 2 6 7 3 8 9 Now I have 3 records in database. I need to create 3 timers and set intervals accordingly. For every 5 secs I should get details regarding ConsumerID-5 For every 7 secs I should get details regarding ConsumerID-6 For every 9 secs I should get details regarding ConsumerID-8 Thanks
-
Hi Thanks a lot for reoplying so soon I cant tell the requirement completely. Let me explain in brief.In database I have a set of records in this structure. ID ConsumerID Time Interval 1 5 5 2 6 7 3 8 9 Now I have 3 records in database. I need to create 3 timers and set intervals accordingly. For every 5 secs I should get details regarding ConsumerID-5 For every 7 secs I should get details regarding ConsumerID-6 For every 9 secs I should get details regarding ConsumerID-8 Thanks
Ok well you could go with Davey's idea of inheritance or you could use the timer's 'Tag' property as follows... when creating...
Timer t = new Timer();
//add event handlers and what not
t.Tag = contactID;then in event handler...
Timer t = (Timer)sender;
if(t.Tag != null)
{
string contactID = (string)t.Tag;
//then do some DB stuff to get the contact details use contactID
}Life goes very fast. Tomorrow, today is already yesterday.
-
Ok well you could go with Davey's idea of inheritance or you could use the timer's 'Tag' property as follows... when creating...
Timer t = new Timer();
//add event handlers and what not
t.Tag = contactID;then in event handler...
Timer t = (Timer)sender;
if(t.Tag != null)
{
string contactID = (string)t.Tag;
//then do some DB stuff to get the contact details use contactID
}Life goes very fast. Tomorrow, today is already yesterday.
The System.Timers.Timer doesn't have a Tag property - only System.Windows.Forms.Timer - an int ID property for each MyTimer : System.Timers.Timer would be the easiest way to go here.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
The System.Timers.Timer doesn't have a Tag property - only System.Windows.Forms.Timer - an int ID property for each MyTimer : System.Timers.Timer would be the easiest way to go here.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
You can cast to a Timer from the sender
if(sender is System.Timers.Timer)
{
System.Timers.Timer thisTimer = sender as System.Timers.Timer;
}If you want you can give it a property such as an int for ID or a string for Name etc by subclassing e.g.
public class MyTimer : System.Timers.Timer
{
public string Name
{
get;
set;
}
}... and then use
MyTimer
instead ofTimer
.Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thanks a lot Dav I got it by using your method
-
Thanks a lot Dav I got it by using your method
No problem, you're welcome :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Thanks a lot Dav I got it by using your method