call a function every 5 seconds

  • Thread starter Thread starter Claudia Fong
  • Start date Start date
C

Claudia Fong

Hi,

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

Cheers!

Claudi
 
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
 
Is a web page, do you have a example of the code on how to set the tick
for every 5 seconds?

Cheers!

Claudi
 
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
 
I want to be on server side...

I don't want to write javascript

Cheers!

Claudi
 
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
 
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
 
Hi,

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

Cheers!

Claudi


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
 
Claudia said:
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
 

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

Back
Top