• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Issues with running two timers at the same time

C

Curious

#1
Hi,

Are there known issues with running two timers at the same time?

I have bumped into a problem - I start a first timer at an interval of
5 seconds. 15 seconds later, I start a second timer at 15 seconds
interval. That's when things start to fail with the first timer (it
doesn't seem to do what it should do).

My seconds question: Is there a way to create a timer for a fixed time
period, say from 9:30 AM to 10 AM?

Any input, advice would be welcome! Thanks!
 

My Computer

F

Family Tree Mike

#2
What do you mean when you say the first timer doesn't do what it should do?
A timer just fires Elapsed events and I have not had problems using multiple
timers. Is it possible the two handlers are running into a conflict in your
code?

On your second question, in the handler you should check the current time
against your desired acceptable window. If you stopped the timer, you would
need a way to start it again, so it is easier just to let it run and check
the clock time.


"Curious" wrote:

> Hi,
>
> Are there known issues with running two timers at the same time?
>
> I have bumped into a problem - I start a first timer at an interval of
> 5 seconds. 15 seconds later, I start a second timer at 15 seconds
> interval. That's when things start to fail with the first timer (it
> doesn't seem to do what it should do).
>
> My seconds question: Is there a way to create a timer for a fixed time
> period, say from 9:30 AM to 10 AM?
>
> Any input, advice would be welcome! Thanks!
>
>
>
 

My Computer

C

Curious

#3
On May 9, 12:01 pm, Family Tree Mike
<FamilyTreeM...@xxxxxx> wrote:

>>>> What do you mean when you say the first timer doesn't do what it shoulddo?  
The first timer grabs the opening prices of a selected set of stock
symbols. For instance, if the opening price of IBM is $125 and the
opening trade happens at 9:31:00 AM, the first timer should grab this
price ($125) for IBM at 9:31:00 AM.

Then the second timer is launched. I noticed that the first timer is
not able to grab the opening prices any more, although there are a lot
of opening trades happening. It feels like that the second timer blows
off the first timer.

>>>> Is it possible the two handlers are running into a conflict in your code?
I don't think so. The first timer grabs the open before the open. Once
open, it grabs the open and that's when its mission is over.

The second timer is a post-open checker. It checks subsequent trades
after the open. So there's no conflict between the two.

>  If you stopped the timer, you would need a way to start it again
Why? If I only want a timer to run for two hours, is there a way to
add this as additional parameter? There may be side effect to let a
timer run for a prolonged period of time when you don't really need
it. It exhausts the system resources.

Thanks,
 

My Computer

F

Family Tree Mike

#4
It sounds like you could do with just one timer, setting the initial stock
price if this is the first timer event after 9:30am and setting the current
if it is later.

Do you have separate event handler code for each timer?


"Curious" wrote:

> On May 9, 12:01 pm, Family Tree Mike
> <FamilyTreeM...@xxxxxx> wrote:

> >>>> What do you mean when you say the first timer doesn't do what it should do?
>
> The first timer grabs the opening prices of a selected set of stock
> symbols. For instance, if the opening price of IBM is $125 and the
> opening trade happens at 9:31:00 AM, the first timer should grab this
> price ($125) for IBM at 9:31:00 AM.
>
> Then the second timer is launched. I noticed that the first timer is
> not able to grab the opening prices any more, although there are a lot
> of opening trades happening. It feels like that the second timer blows
> off the first timer.
>

> >>>> Is it possible the two handlers are running into a conflict in your code?
>
> I don't think so. The first timer grabs the open before the open. Once
> open, it grabs the open and that's when its mission is over.
>
> The second timer is a post-open checker. It checks subsequent trades
> after the open. So there's no conflict between the two.
>

> > If you stopped the timer, you would need a way to start it again
>
> Why? If I only want a timer to run for two hours, is there a way to
> add this as additional parameter? There may be side effect to let a
> timer run for a prolonged period of time when you don't really need
> it. It exhausts the system resources.
>
> Thanks,
>
 

My Computer

F

Family Tree Mike

#5
Perhaps the following console app code will show how two timers can
cohabitate...

private static Timer FirstTimer = new Timer();
private static Timer SecondTimer = new Timer();
private static Boolean OpeningBellRang = false;
private static int Counter = 0;
private static Random Randomizer = new Random();

static void Main(string[] args)
{
FirstTimer.Interval = 1000;
FirstTimer.Elapsed += new ElapsedEventHandler(FirstTimer_Elapsed);
FirstTimer.Start();

while (Counter < 10)
{
System.Threading.Thread.Sleep(500);
}
}

static Boolean SomeRandomChecker()
{
return (Randomizer.Next(10) > 5);
}

static void FirstTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
Console.Out.WriteLine("Timer one fired...");
++Counter;

if (!OpeningBellRang & SomeRandomChecker())
{
Console.Out.WriteLine("Starting timer two...");
OpeningBellRang = true;
SecondTimer.Interval = 1500;
SecondTimer.Elapsed += new ElapsedEventHandler(SecondTimer_Elapsed);
SecondTimer.Start();
}
}

static void SecondTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.Out.WriteLine("Timer two fired...");
}
 

My Computer

C

Curious

#6
Thanks for the input!

I do have different event handlers for each timer.

I was thinking of combining the two timers into one as well by setting
the interval to be 5 seconds. However, that may be a waste, because
the first timer only runs for about hour an hour while the second
timer (with an interval of 15 seconds) runs throughout the day.

If I combine the two into one, I'll only fire the event for the second
timer when it's on the 15 seconds (the 3rd time when the timer ticks
because the interval is 5 seconds).
 

My Computer

C

Curious

#7
I do have a different event handler for each timer.

I was thinking of combining the two timers into one as well by
setting
the interval to be 5 seconds. However, that may be a waste, because
the first timer only runs for about half an hour, while the second
timer (with an interval of 15 seconds) runs throughout the day.

If I combine the two into one, I'll only fire the event for the
second
timer when it's on the 15 seconds (the 3rd time when the timer ticks
because the interval of the consolidated timer is 5 seconds).
 

My Computer

F

Family Tree Mike

#8
The timer interval can be changed after the timer is started. Just change it
after the first time.

"Curious" wrote:

> I do have a different event handler for each timer.
>
> I was thinking of combining the two timers into one as well by
> setting
> the interval to be 5 seconds. However, that may be a waste, because
> the first timer only runs for about half an hour, while the second
> timer (with an interval of 15 seconds) runs throughout the day.
>
> If I combine the two into one, I'll only fire the event for the
> second
> timer when it's on the 15 seconds (the 3rd time when the timer ticks
> because the interval of the consolidated timer is 5 seconds).
>
 

My Computer

C

Curious

#9
On May 10, 10:10 pm, Family Tree Mike
<FamilyTreeM...@xxxxxx> wrote:

> The timer interval can be changed after the timer is started.  Just change it
> after the first time.
>
That's true. Thanks for the hint!
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)