Timer in Console application

A

Aart Nicolai

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
 
A

Aart Nicolai

I forgot to add the code.
<cut>
Dim TimerInterval As Integer = m_Settings.GetValue("TimerInterval",
GetType(String))
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = TimerInterval
aTimer.Enabled = True
aTimer.AutoReset = True
Console.WriteLine("Wacht op aflopen timer [" &
m_Settings.GetValue("TimerInterval", GetType(Integer)) / 1000 & "
sec.]")
Console.Read()
</cut>
 
S

Simon Verona

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

ie
do
threading.threading.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 said:
why not just replace with an infinite loop with a sleep in?


Aart Nicolai said:
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
 
G

Guest

or even better, the multimedia timer tha runs in its own thread that
you configure as a callback function
or better still, using the threading class'es timer within your loop
ie
do
threading.threading.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 said:
why not just replace with an infinite loop with a sleep in?


Aart Nicolai said:
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
 
J

Jay B. Harlow [MVP - Outlook]

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 ManualResetEvent (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.Timer 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


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

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