Problem with timers on web user control

J

Jozef

Hi!

I've made web user control and put two timers, update panel and label control.

First timer - Timer1 - has interval 1s
Seconf timer - Time2 - has interval 5s

Also I have method to start timers, let we call it Start()

inside Start()

Timer1.Interval = 1000;
Timer1.Enabled = true;

Timer2.Interval = 5000;
Timer2.Enabled = true;


Inside Timer1_Tick i have something that do every 1s, also
the same thing is in Timer2_Tick but it executes every 5s.

Problem is that Timer1 works fine, but Timer2 works nothing.
I put breakpoint inside Timer2_Tick but nothing happens.
If i put inside Timer1_Tick program breaks on that line.

I'm little bit confused about that. Is it possible that two timers
can't work on same web user control? Or I'm doing something wrong?
 
A

Arne Vajhøj

I've made web user control and put two timers, update panel and label
control.

First timer - Timer1 - has interval 1s
Seconf timer - Time2 - has interval 5s

Also I have method to start timers, let we call it Start()

inside Start()

Timer1.Interval = 1000;
Timer1.Enabled = true;

Timer2.Interval = 5000;
Timer2.Enabled = true;


Inside Timer1_Tick i have something that do every 1s, also
the same thing is in Timer2_Tick but it executes every 5s.

Problem is that Timer1 works fine, but Timer2 works nothing.
I put breakpoint inside Timer2_Tick but nothing happens.
If i put inside Timer1_Tick program breaks on that line.

I'm little bit confused about that. Is it possible that two timers
can't work on same web user control? Or I'm doing something wrong?

It is not quite clear how your code looks like and .NET has multiple
Timer classes (this looks like a System.Timers.Timer but ...) - could
you post a complete example of what you are trying to do?

Arne

PS: I would not consider using timers in a web app to be
a good practice, but that is another discussion.
 
J

Jozef

It is not quite clear how your code looks like and .NET has multiple
Timer classes (this looks like a System.Timers.Timer but ...) - could
you post a complete example of what you are trying to do?

I'm working on example but as I wrote in first post i've stucked on
begining.

Here is the main part of my code:


public void StartTimers()
{
Timer1.Interval = 1000;
Timer1.Enabled = true;

Timer2.Interval = 5000;
Timer2.Enabled = true;

}
protected void Timer1_Tick(....)
{
DoSomething1();
}


protected void Timer2_Tick(...)
{
DoSomething2();
}


private void DoSomething1()
{
Label1.Text = "Timer 1";
}

private void DoSomething2()
{
Label1.Text = "Timer 2";
}


Label control is on updatepanel
UpdatePanel has Trigger configured on AyncPostBack:Timer1.Tick


What to do? Is there any other solution that might help.
 
J

Jozef

[...]
What to do? Is there any other solution that might help.

Did you read my other reply?

If you must have two different timer intervals, I recommend you use a single timer with the shorter interval (of 1
second in this case), but keep a counter to track the second operation. Every fifth time the timer elapses and your
handler is called, do both the 1 second and the 5 second behaviors. Otherwise, just do the 1 second behavior.

Pete

uh... i'm not sure if i understand well.. Can you put some example? How to keep a counter to
track the second operation? I'm little bit confused...
 
J

Jozef

I think i made it...


static int x = 0;


and inside Timer1_Tick(....)

x++;

if (x==5)
{
DoSomething();
}



and this works.

but that if i change timer interval to 5s and i want to run DoSomething() every 6s?
Will then DoSomething() run every 11s?
 
A

Arne Vajhøj

[...]
PS: I would not consider using timers in a web app to be
a good practice, but that is another discussion.

Although, since that's good advice, then if the problem the OP is really
having trouble with the multiple timers, then avoiding timers altogether
could solve his problem. :)
True.

There is a Web Forms Timer class:
http://msdn.microsoft.com/en-us/library/system.web.ui.timer.aspx

I note in the documentation this sentence: "If a new postback is
initiated while an earlier postback is being processed, the first
postback will be canceled." Since 5 is a multiple of 1, it stands to
reason that at the 5 second interval, both postbacks will be attempted
at the same time. Only one will win.

It could be it.

Arne
 
A

Arne Vajhøj

I'm working on example but as I wrote in first post i've stucked on
begining.

Here is the main part of my code:

public void StartTimers()
{
Timer1.Interval = 1000;
Timer1.Enabled = true;

Timer2.Interval = 5000;
Timer2.Enabled = true;

}
protected void Timer1_Tick(....)
{
DoSomething1();
}


protected void Timer2_Tick(...)
{
DoSomething2();
}


private void DoSomething1()
{
Label1.Text = "Timer 1";
}

private void DoSomething2()
{
Label1.Text = "Timer 2";
}

Label control is on updatepanel
UpdatePanel has Trigger configured on AyncPostBack:Timer1.Tick

That does not show what Timer1 and Timer2 is declared as.

But probably Peters idea about what those are is correct
and his suggestion for fix sounds very plausible.

Arne
 
A

Arne Vajhøj

I think i made it...

static int x = 0;

and inside Timer1_Tick(....)

x++;

if (x==5)
{
DoSomething();
}

and this works.

but that if i change timer interval to 5s and i want to run
DoSomething() every 6s?
Will then DoSomething() run every 11s?

The interval you run the timer at need to be a divisor
of all action intervals.

Arne
 
A

Arne Vajhøj

I think i made it...

static int x = 0;

and inside Timer1_Tick(....)

x++;

if (x==5)
{
DoSomething();
}

and this works.

but that if i change timer interval to 5s and i want to run
DoSomething() every 6s?
Will then DoSomething() run every 11s?

Oh - and the above code may work for single user, but it
will not work for multiple users.

You should replace that static with something user
specific.

Maybe view state could be used.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top