471,339 Members | 1,246 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

call a function every 5 seconds

Hi,

I want to call a function every 5 seconds, is it possible?
How to?

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
11 9097
Well, you can have a timer set to tick every 5 seconds, and handle the
event that gets fired? There are two such timers depending on whether it
is for winform or server usage.

Alternatively, you could have a worker thread looping with a Sleep(5000)?

Marc
Jun 27 '08 #2
Is a web page, do you have a example of the code on how to set the tick
for every 5 seconds?

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #3
At client side? Well there are javascript ways of doing this in the
browser - setInterval / setTimeout:

http://www.google.com/search?q=setInterval+asp.net

Marc
Jun 27 '08 #4
Sorry - I should have added; for more information, you might want to try
asking on one of the ASP.NET forums, but a clientside javascript call is
a bit OT for a C# language forum.

Marc
Jun 27 '08 #5
I want to be on server side...

I don't want to write javascript

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #6
Well, short of a meta-refresh, you can't.

A html page lives at the client. The server can't do anything once the
page has gone to the client - the client has to deal with that.

Marc
Jun 27 '08 #7
On Thu, 22 May 2008 05:29:05 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
Well, you can have a timer set to tick every 5 seconds, and handle the
event that gets fired? There are two such timers depending on whether it
is for winform or server usage.
Nitpick: not that this helps the OP, but there are at least three timer
classes in .NET. They are in System.Timers, System.Threading, and
System.Windows.Forms.

Pete
Jun 27 '08 #8
On May 22, 12:54*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 22 May 2008 05:29:05 -0700, Marc Gravell <marc.grav...@gmail.com*
wrote:
Well, you can have a timer set to tick every 5 seconds, and handle the *
event that gets fired? There are two such timers depending on whether it*
is for winform or server usage.

Nitpick: not that this helps the OP, but there are at least three timer *
classes in .NET. *They are in System.Timers, System.Threading, and *
System.Windows.Forms.

Pete
I need to create a program for a car racer

Jun 27 '08 #9
On Thu, 22 May 2008 05:21:30 -0700, Claudia Fong wrote:
Hi,

I want to call a function every 5 seconds, is it possible? How to?

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***

Trying using Thread.Sleep to "pause" the current thread.

using System.Threading;
public class Test
{
public Test()
{
Thread everyFiveSeconds = new Thread
(FunctionToRunEveryFiveSeconds);
updater.Name = "Updater - Thread";
updater.Start();

}

public FunctionToRunEveryFiveSeconds()
{
object.Do();
Thread.Sleep(5000);
}

public SomeOtherWork()
{
while(true)
{
object.DoSomethingElse();
}
}
}
Regards j1mb0jay
Jun 27 '08 #10
On May 22, 3:22 pm, Claudia Fong <cdolphi...@yahoo.co.ukwrote:
I want to be on server side...

I don't want to write javascript

Cheers!

Claudi

*** Sent via Developersdexhttp://www.developersdex.com***
I think that you can use ajax. But this is based on javascript.
Jun 27 '08 #11
Claudia Fong wrote:
I want to be on server side...

I don't want to write javascript
Doing something server side every N second does not
fit well with the ASP.NET model.

Options I can see:

1) Start a thread (like in global.asax Application_Start) that
updates some global data (like stored in a singleton) every N
second and pages gets info from that global data.

2) Run a Windows service that has the thread that updates some
data every N seconds and have pages request info via
a low overhead protocol (remoting, plain sockets etc.).

#1 is not good (in general it is not good to mess around
with threads in a context where the container manage threads
and there will be extra work to get it to work well over web app
restarts) and #2 is rather complex, but I think
that is the options.

Arne
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Vedran Furac | last post: by
5 posts views Thread by Lucy Randles | last post: by
4 posts views Thread by Dave | last post: by
12 posts views Thread by leaf | last post: by
4 posts views Thread by Frank Lund | last post: by
reply views Thread by rosydwin | last post: by

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.