call a function every 5 seconds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claudia Fong

    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 ***
  • Marc Gravell

    #2
    Re: call a function every 5 seconds

    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

    Comment

    • Claudia Fong

      #3
      Re: call a function every 5 seconds

      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 ***

      Comment

      • Marc Gravell

        #4
        Re: call a function every 5 seconds

        At client side? Well there are javascript ways of doing this in the
        browser - setInterval / setTimeout:



        Marc

        Comment

        • Marc Gravell

          #5
          Re: call a function every 5 seconds

          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

          Comment

          • Claudia Fong

            #6
            Re: call a function every 5 seconds

            I want to be on server side...

            I don't want to write javascript

            Cheers!

            Claudi

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

            Comment

            • Marc Gravell

              #7
              Re: call a function every 5 seconds

              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

              Comment

              • Peter Duniho

                #8
                Re: call a function every 5 seconds

                On Thu, 22 May 2008 05:29:05 -0700, Marc Gravell <marc.gravell@g mail.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.Threadin g, and
                System.Windows. Forms.

                Pete

                Comment

                • mide

                  #9
                  Re: call a function every 5 seconds

                  On May 22, 12:54 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
                  wrote:
                  On Thu, 22 May 2008 05:29:05 -0700, Marc Gravell <marc.grav...@g mail.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.Threadin g, and  
                  System.Windows. Forms.
                  >
                  Pete
                  I need to create a program for a car racer

                  Comment

                  • j1mb0jay

                    #10
                    Re: call a function every 5 seconds

                    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.Threadin g;
                    public class Test
                    {
                    public Test()
                    {
                    Thread everyFiveSecond s = new Thread
                    (FunctionToRunE veryFiveSeconds );
                    updater.Name = "Updater - Thread";
                    updater.Start() ;

                    }

                    public FunctionToRunEv eryFiveSeconds( )
                    {
                    object.Do();
                    Thread.Sleep(50 00);
                    }

                    public SomeOtherWork()
                    {
                    while(true)
                    {
                    object.DoSometh ingElse();
                    }
                    }
                    }


                    Regards j1mb0jay

                    Comment

                    • deccio

                      #11
                      Re: call a function every 5 seconds

                      On May 22, 3:22 pm, Claudia Fong <cdolphi...@yah oo.co.ukwrote:
                      I want to be on server side...
                      >
                      I don't want to write javascript
                      >
                      Cheers!
                      >
                      Claudi
                      >
                      *** Sent via Developersdexht tp://www.developersd ex.com***
                      I think that you can use ajax. But this is based on javascript.

                      Comment

                      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                        #12
                        Re: call a function every 5 seconds

                        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_Sta rt) 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

                        Comment

                        Working...