Timer in Console application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aart Nicolai

    Timer in Console application

    Hi all,

    I have developed a vb.net console application which will run some code
    every [x] seconds. To get this working I used a timer "System.Timers" .
    When I start my console application it exits within a second. So I
    added the line: "Console.Read() ". Everything seemes to run fine but
    after a few days the application stopped. Without an error.
    I think it has something to do with the lifetime of the
    "Console.Read() ". Is this right?

    Is there another trick to solve this timer problem?

    Thanks,

    Aart

  • Aart Nicolai

    #2
    Re: Timer in Console application

    I forgot to add the code.
    <cut>
    Dim TimerInterval As Integer = m_Settings.GetV alue("TimerInte rval",
    GetType(String) )
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    aTimer.Interval = TimerInterval
    aTimer.Enabled = True
    aTimer.AutoRese t = True
    Console.WriteLi ne("Wacht op aflopen timer [" &
    m_Settings.GetV alue("TimerInte rval", GetType(Integer )) / 1000 & "
    sec.]")
    Console.Read()
    </cut>

    Comment

    • Simon Verona

      #3
      Re: Timer in Console application

      why not just replace with an infinite loop with a sleep in?


      "Aart Nicolai" <aartnicolai@gm ail.com> wrote in message
      news:1135859142 .317096.322660@ g43g2000cwa.goo glegroups.com.. .[color=blue]
      > Hi all,
      >
      > I have developed a vb.net console application which will run some code
      > every [x] seconds. To get this working I used a timer "System.Timers" .
      > When I start my console application it exits within a second. So I
      > added the line: "Console.Read() ". Everything seemes to run fine but
      > after a few days the application stopped. Without an error.
      > I think it has something to do with the lifetime of the
      > "Console.Read() ". Is this right?
      >
      > Is there another trick to solve this timer problem?
      >
      > Thanks,
      >
      > Aart
      >[/color]


      Comment

      • Simon Verona

        #4
        Re: Timer in Console application

        or better still, using the threading class'es timer within your loop

        ie
        do
        threading.threa ding.sleep(5000 ) ' sleep for 5 seconds

        ' Do your stuff here

        loop until false ' I think you can use this for an infinite loop
        "Simon Verona" <nomail@nomail. zzz> wrote in message
        news:u2zLFWIDGH A.2664@TK2MSFTN GP15.phx.gbl...[color=blue]
        > why not just replace with an infinite loop with a sleep in?
        >
        >
        > "Aart Nicolai" <aartnicolai@gm ail.com> wrote in message
        > news:1135859142 .317096.322660@ g43g2000cwa.goo glegroups.com.. .[color=green]
        >> Hi all,
        >>
        >> I have developed a vb.net console application which will run some code
        >> every [x] seconds. To get this working I used a timer "System.Timers" .
        >> When I start my console application it exits within a second. So I
        >> added the line: "Console.Read() ". Everything seemes to run fine but
        >> after a few days the application stopped. Without an error.
        >> I think it has something to do with the lifetime of the
        >> "Console.Read() ". Is this right?
        >>
        >> Is there another trick to solve this timer problem?
        >>
        >> Thanks,
        >>
        >> Aart
        >>[/color]
        >
        >[/color]


        Comment

        • g9u5dd43_nospam@yahoo.com

          #5
          Re: Timer in Console application

          On Thu, 29 Dec 2005 15:17:20 -0000, "Simon Verona" <nomail@nomail. zzz>
          wrote:

          or even better, the multimedia timer tha runs in its own thread that
          you configure as a callback function
          [color=blue]
          >or better still, using the threading class'es timer within your loop
          >ie
          >do
          > threading.threa ding.sleep(5000 ) ' sleep for 5 seconds
          > ' Do your stuff here
          >
          >loop until false ' I think you can use this for an infinite loop
          >"Simon Verona" <nomail@nomail. zzz> wrote in message
          >news:u2zLFWIDG HA.2664@TK2MSFT NGP15.phx.gbl.. .[color=green]
          >> why not just replace with an infinite loop with a sleep in?
          >>
          >>
          >> "Aart Nicolai" <aartnicolai@gm ail.com> wrote in message
          >> news:1135859142 .317096.322660@ g43g2000cwa.goo glegroups.com.. .[color=darkred]
          >>> Hi all,
          >>>
          >>> I have developed a vb.net console application which will run some code
          >>> every [x] seconds. To get this working I used a timer "System.Timers" .
          >>> When I start my console application it exits within a second. So I
          >>> added the line: "Console.Read() ". Everything seemes to run fine but
          >>> after a few days the application stopped. Without an error.
          >>> I think it has something to do with the lifetime of the
          >>> "Console.Read() ". Is this right?
          >>>
          >>> Is there another trick to solve this timer problem?
          >>>
          >>> Thanks,
          >>>
          >>> Aart
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Timer in Console application

            Aart,
            Rather then use a Console application, have you considered a Windows Service
            application?

            As a Windows Service are designed to run "forever", plus they can be started
            when the machine starts, rather then require someone to log in.

            If your app "needs" to be a Console application, as the others suggests, you
            need to prevent your Main from exiting. I would probably use a Mutex,
            AutoResetEvent, or ManualResetEven t (favoring the reset events over the
            mutex) to keep it from exiting. This way the timer thread can have the main
            thread exit if the timer thread decides its "done". By timer thread I mean
            the thread where the System.Timers.T imer Elapsed event is raised...

            Post if you want help with the reset events or the windows service.


            --
            Hope this helps
            Jay [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Aart Nicolai" <aartnicolai@gm ail.com> wrote in message
            news:1135859142 .317096.322660@ g43g2000cwa.goo glegroups.com.. .
            | Hi all,
            |
            | I have developed a vb.net console application which will run some code
            | every [x] seconds. To get this working I used a timer "System.Timers" .
            | When I start my console application it exits within a second. So I
            | added the line: "Console.Read() ". Everything seemes to run fine but
            | after a few days the application stopped. Without an error.
            | I think it has something to do with the lifetime of the
            | "Console.Read() ". Is this right?
            |
            | Is there another trick to solve this timer problem?
            |
            | Thanks,
            |
            | Aart
            |


            Comment

            • Aart Nicolai

              #7
              Re: Timer in Console application

              Thanks, I think I will transform it into a windows service..

              Comment

              Working...