VB.Net Console app needs timer

S

Scott M.

Hi,

I'm an old VB6 guy just starting out in VB.Net using Visual Studio
Express. I want to build a simple console app that will create a
simple text file every 10 minutes. I can create the file OK, but how
do I put in the timer so that I can write the file on schedule? The
idea behind this project is to help me monitor remote computers in a
production environment to make sure that they are up and running. I
currently synchronize files on them using DriveHQ.com. What I want to
do is be able to examine my DriveHQ account and see, from what I write
in the text file, that my systems are up and running.

The app coding so far is this;

'create file
My.Computer.FileSystem.WriteAllText("C:\Test.txt",
String.Empty, False)
'write file header
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Heartbeat
File" & vbCrLf, True)
'write current date/time to file
My.Computer.FileSystem.WriteAllText("C:\Test.txt", Now() &
vbCrLf, True)


Scott
 
R

rowe_newsgroups

Hi,

I'm an old VB6 guy just starting out in VB.Net using Visual Studio
Express. I want to build a simple console app that will create a
simple text file every 10 minutes. I can create the file OK, but how
do I put in the timer so that I can write the file on schedule? The
idea behind this project is to help me monitor remote computers in a
production environment to make sure that they are up and running. I
currently synchronize files on them using DriveHQ.com. What I want to
do is be able to examine my DriveHQ account and see, from what I write
in the text file, that my systems are up and running.

The app coding so far is this;

'create file
My.Computer.FileSystem.WriteAllText("C:\Test.txt",
String.Empty, False)
'write file header
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Heartbeat
File" & vbCrLf, True)
'write current date/time to file
My.Computer.FileSystem.WriteAllText("C:\Test.txt", Now() &
vbCrLf, True)

Scott

This may not be the "proper" way - but I never cared about being
proper. :)

I would first add a reference to System.Windows.Forms to my project
and do this:

Imports System.Windows.Forms

Module Module1

Sub Main()
Dim t As New Timer
t.Interval = 600000 '// I think 10 minutes = 600,000
milliseconds :)
AddHandler t.Tick, AddressOf timer_Tick
t.Start()

Application.Run()
End Sub

Public Sub timer_Tick(ByVal sender As Object, ByVal e As
EventArgs)
Using writer As New System.IO.StreamWriter("C:\Test.txt")
writer.WriteLine("Heartbeat File")
writer.WriteLine(DateTime.Now)
End Using
End Sub

End Module

Thanks,

Seth Rowe
 
R

rowe_newsgroups

This may not be the "proper" way - but I never cared about being
proper. :)

I would first add a reference to System.Windows.Forms to my project
and do this:

Imports System.Windows.Forms

Module Module1

Sub Main()
Dim t As New Timer
t.Interval = 600000 '// I think 10 minutes = 600,000
milliseconds :)
AddHandler t.Tick, AddressOf timer_Tick
t.Start()

Application.Run()
End Sub

Public Sub timer_Tick(ByVal sender As Object, ByVal e As
EventArgs)
Using writer As New System.IO.StreamWriter("C:\Test.txt")
writer.WriteLine("Heartbeat File")
writer.WriteLine(DateTime.Now)
End Using
End Sub

End Module

Thanks,

Seth Rowe

I forgot to add - using the System.Windows.Forms timer in a console
project is considered a no-no by some - you can use the other
available timers if you want, but I prefer the simplicity of the Forms
timer.

Thanks,

Seth Rowe
 
A

Al G

Scott M. said:
I am too new to VB2005 to know how to use it. Can you give me a hint?

Start Menu > Settings > Control Panel > Scheduled Tasks > Add Task...

Al G
 
S

Scott M.

Start Menu > Settings > Control Panel > Scheduled Tasks > Add Task...

Al G- Hide quoted text -

- Show quoted text -

Oh, that scheduler. I can use it to launch my tiny program, but I need
the program to write out my heartbeat file every ten minutes. I could
just as easily put my program in the startup folder.
 
M

Mr. Arnold

Scott M. said:
Oh, that scheduler. I can use it to launch my tiny program, but I need
the program to write out my heartbeat file every ten minutes. I could
just as easily put my program in the startup folder.

What's wrong with using System.Timers name space to create a timer with an
AddressHandler for the time elasped. I done it in console applications.
 
C

Chris Dunaway

Oh, that scheduler. I can use it to launch my tiny program, but I need
the program to write out my heartbeat file every ten minutes. I could
just as easily put my program in the startup folder.

But that will only launch your program once. Set the scheduled task
to run your program every 10 minutes. That way, all you need to do in
your program is just create the file and you don't have to worry about
the timer.

Chris
 
S

Scott M.

But that will only launch your program once. Set the scheduled task
to run your program every 10 minutes. That way, all you need to do in
your program is just create the file and you don't have to worry about
the timer.

Chris- Hide quoted text -

- Show quoted text -

I agree with you Chris, but I've looked at Scheduler and I don't think
I can launch a task any more than once per day. Try it by setting up
NotePad or some other app to run, just to see how often you can get it
to run.

Scott
 
M

Mark S. Milley, MCSD (BinarySwitch)

Hi Scott -

To repeat a task: In the task scheduler, after you create the task, go
to the task's properties, then click advanced. There's a checkbox to
Repeat the Task, and, conincidentally, the default is every 10
minutes.

My personal take on which timer you should use: I'm a purist, and I
would go with the System.Timers one, since you are not creating a
WinForms app. As I understand it, the Windows Forms version has more
overhead (not that most people care about memory usage these days.)

That said, unless you have some other reason to have your application
running all day long, I would do the scheduled task; if you would
prefer to have it running 24/7, maybe you should look into creating a
windows service (is this possible in express?) instead of a console
app....

Cheers,

-Mark
 
R

rowe_newsgroups

Hi Scott -

To repeat a task: In the task scheduler, after you create the task, go
to the task's properties, then click advanced. There's a checkbox to
Repeat the Task, and, conincidentally, the default is every 10
minutes.

My personal take on which timer you should use: I'm a purist, and I
would go with the System.Timers one, since you are not creating a
WinForms app. As I understand it, the Windows Forms version has more
overhead (not that most people care about memory usage these days.)

That said, unless you have some other reason to have your application
running all day long, I would do the scheduled task; if you would
prefer to have it running 24/7, maybe you should look into creating a
windows service (is this possible in express?) instead of a console
app....

Cheers,

-Mark

windows service (is this possible in express?)

I think it is - after all isn't everything included with the .Net
framework and not the ide? I mean in reality you could even just write
the service with notepad and vbc.exe right?

Thanks,

Seth Rowe
 
M

Mr. Arnold

windows service (is this possible in express?) instead of a console

Yes, you can create service application with Express, by coding it yourself.
There is no windows service template for Express, like there is for
Professional. There are examples out on Google on how to code the service,
in VB or C#.NET.
 
S

Scott M.

Yes, you can create service application with Express, by coding it yourself.
There is no windows service template for Express, like there is for
Professional. There are examples out on Google on how to code the service,
in VB or C#.NET.

Hi all,

I appreciate all your advice. I decided against a console app because
I don't want to see a DOS box pop up. All I want to do is write out a
simple file that holds something like;

Heartbeat File
06/07/2007 1:25:59 PM

in it and then overwrites it 10 minutes later (or 20 or 30 or one
hour, it doesn't really matter). I can monitor this file remotely and
I will know if the computer is up and running by looking at the last
timestamp written in the file. I don't need a user interface or a
system tray icon or anything. I have never created a service
application but I will try to figure out how to do that. It seems like
a good solution. Barring that, I can make a simple Windows app, make
it hide itself and use the scheduler as Mark indicates.

This project must sound pretty lame to you guys, but I am just getting
my feet wet with Visual Studio and VB6 hasn't prepared me at all for
the realities of true OO Programming. I even signed up with
LearnVisualStudio.net to start my learning curve.

Scott
 
R

rowe_newsgroups

Hi all,

I appreciate all your advice. I decided against a console app because
I don't want to see a DOS box pop up. All I want to do is write out a
simple file that holds something like;

Heartbeat File
06/07/2007 1:25:59 PM

in it and then overwrites it 10 minutes later (or 20 or 30 or one
hour, it doesn't really matter). I can monitor this file remotely and
I will know if the computer is up and running by looking at the last
timestamp written in the file. I don't need a user interface or a
system tray icon or anything. I have never created a service
application but I will try to figure out how to do that. It seems like
a good solution. Barring that, I can make a simple Windows app, make
it hide itself and use the scheduler as Mark indicates.

This project must sound pretty lame to you guys, but I am just getting
my feet wet with Visual Studio and VB6 hasn't prepared me at all for
the realities of true OO Programming. I even signed up with
LearnVisualStudio.net to start my learning curve.

Scott

I'm saying a service would be overkill for this - and if you're just
learning you may want to steer clear of services for a bit - they can
get a bit tricky. My personal recommendation would be to create the
following exe and execute it with the task scheduler like so:

1) Start a New Window's Application project
2) Delete the default form it creates (usually called Form1)
3) Add a new module to the project called Module1 (or whatever)
4) Put your code to write the file in the Sub Main() method
5) Go to the Application tab on your project's properties (Project--
MyProjectName Properties)
6) Uncheck "Enable application framework"
7) Select "Module1" from the Startup Object drop down
8) Rebuild your app and deploy the exe (usually found in My Documents--
Visual Studio 2005-->Project-->MySolutionName-->MyProjectName-->bin--
release-->*.exe)
9) Schedule your task to run every ten minutes
10) Grab a beer and relax (I needed a tenth step :))

Thanks,

Seth Rowe
 
S

Scott M.

I'm saying a service would be overkill for this - and if you're just
learning you may want to steer clear of services for a bit - they can
get a bit tricky. My personal recommendation would be to create the
following exe and execute it with the task scheduler like so:

1) Start a New Window's Application project
2) Delete the default form it creates (usually called Form1)
3) Add a new module to the project called Module1 (or whatever)
4) Put your code to write the file in the Sub Main() method
5) Go to the Application tab on your project's properties (Project-->MyProjectName Properties)

6) Uncheck "Enable application framework"
7) Select "Module1" from the Startup Object drop down
8) Rebuild your app and deploy the exe (usually found in My Documents-->Visual Studio 2005-->Project-->MySolutionName-->MyProjectName-->bin--

9) Schedule your task to run every ten minutes
10) Grab a beer and relax (I needed a tenth step :))

Thanks,

Seth Rowe- Hide quoted text -

- Show quoted text -

Thanks Seth,

I followed all of your steps, except 10. That will come later.
Certainly going to .Net makes for a more complicated world. I ran a
build and took the exe to my target machine. I tried to run it, but
since it did not have the .net framework on it, I got a nasty message.
OK, so then I go back and run Publish, creating a large directory of
files. I took this to my target machine and ran setup. Of course, I
had to accept giving Microsoft my first born (too late) and then for
10 minutes or so it downloaded a bunch of files. I then was able to
run the setup. It took so long that I walked away. When I returned, I
found that my application, Heartbeat.exe, had cause great grief and I
was asked if I wanted to send Bill an error report. I chose No because
he has never responded to any other error report.

So, I went back to VB6. I created the application is 10 minutes,
created an exe and copied the exe to the target machine. It worked
just fine. I then set up scheduler to run it every 10 minutes. I then
set up my DriveHQ Backup software to back it up to the web. It is all
working fine. The VB project, by the way, is a simple three files (3K,
1K and 1K) with a 22K executable.

I'll still start my Visual Studio training, but the future looks
complicated.

Scott
 
M

Mark S. Milley, MCSD (BinarySwitch)

Hi Scott -

Don't worry, it's actually easier; you're just experiencing a paradigm
shift.

Never give up,

-Mark
 
R

rowe_newsgroups

Thanks Seth,

I followed all of your steps, except 10. That will come later.
Certainly going to .Net makes for a more complicated world. I ran a
build and took the exe to my target machine. I tried to run it, but
since it did not have the .net framework on it, I got a nasty message.
OK, so then I go back and run Publish, creating a large directory of
files. I took this to my target machine and ran setup. Of course, I
had to accept giving Microsoft my first born (too late) and then for
10 minutes or so it downloaded a bunch of files. I then was able to
run the setup. It took so long that I walked away. When I returned, I
found that my application, Heartbeat.exe, had cause great grief and I
was asked if I wanted to send Bill an error report. I chose No because
he has never responded to any other error report.

So, I went back to VB6. I created the application is 10 minutes,
created an exe and copied the exe to the target machine. It worked
just fine. I then set up scheduler to run it every 10 minutes. I then
set up my DriveHQ Backup software to back it up to the web. It is all
working fine. The VB project, by the way, is a simple three files (3K,
1K and 1K) with a 22K executable.

I'll still start my Visual Studio training, but the future looks
complicated.

Scott


Hmm... sorry to hear that your VB.Net application bombed. If it's not
too long would you mind posting the source? Perhaps we could educate
you on what might have went wrong?\.

Thanks,

Seth Rowe
 
G

Guest

I would just add the 1 line of code below to make it "sleep" for a certain
amount of milliseconds. 600,000 milliseconds is approx. 10 minutes.

System.Threading.Thread.Sleep(600000)
 

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