|
-
Apr 1st, 2012, 02:15 PM
#1
[RESOLVED] How do I make a console application continue running
I was thinking I could make a console application that would have a FileSystemWatcher - watching for a .BAT file being created in a folder - and when it sees that file it does a PROCESS.START on it.
Seems simple.
But I'm seeing that a console app doesn't continue running.
Code:
Imports System.IO
Module awcWatcher
Sub Main()
Dim fsw As New FileSystemWatcher("D:\ACS Desktop\AWC\reporting\staging")
fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
AddHandler fsw.Changed, AddressOf fsw_Changed
fsw.EnableRaisingEvents = True
End Sub
Private Sub fsw_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Debug.WriteLine(e.FullPath)
End Sub
End Module
How would I go about making this wait and then wait again and again and again?
-
Apr 1st, 2012, 03:12 PM
#2
Re: How do I make a console application continue running
vb Code:
Imports System.IO
Module awcWatcher
Sub Main()
Dim fsw As New FileSystemWatcher("D:\ACS Desktop\AWC\reporting\staging")
fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
AddHandler fsw.Changed, AddressOf fsw_Changed
fsw.EnableRaisingEvents = True
dim exit as string = console.readline
while exit.tolower <> "exit"
exit = console.readline
end while
End Sub
Private Sub fsw_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Debug.WriteLine(e.FullPath)
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 1st, 2012, 03:24 PM
#3
Re: How do I make a console application continue running
cool - that works...
Which brings me around to another issue - I'm trying to avoid creating a windows service for a down-and-dirty quick fix to a problem I'm having with a asp.net webservice...
So I was going for the console app - how would I go about having this console app "start" when the "server" reboots?
Also - can I avoid the CONSOLE window?
Does that cause your Console.ReadLine fix to fail me?
Do I need to just bite the windows service bullet.
Argh!
-
Apr 1st, 2012, 03:51 PM
#4
Re: How do I make a console application continue running
create a new winforms project + delete form1.
add a module + paste in your code:
vb Code:
Imports System.IO
Module Module1
Public Sub main(ByVal args() As String)
Dim fsw As New FileSystemWatcher("D:\ACS Desktop\AWC\reporting\staging")
fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
AddHandler fsw.Changed, AddressOf fsw_Changed
fsw.EnableRaisingEvents = True
End Sub
Private Sub fsw_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
Debug.WriteLine(e.FullPath)
End Sub
End Module
go to Project-->Properties-->Application + uncheck Enable application framework + set your Startup Object to Sub Main.
to run at startup + keep running until system shutdown (i've never tried this on a server):
HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Run + add a string value where Name is a description + Data is your exe path
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 1st, 2012, 03:58 PM
#5
Re: How do I make a console application continue running
Writing a value to the Run key in the register will only run your application if a user is logged on the server. I think you should go with the Windows Service route.
I have a couple of blog posts on this subject and one shows how you can run your windows service as a console application for debugging purposes.
https://msmvps.com/blogs/joacim/arch...e/default.aspx
-
Apr 1st, 2012, 03:58 PM
#6
Re: How do I make a console application continue running
i noticed, you need to put this at the end of sub main:
vb Code:
While True
Application.DoEvents()
End While
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 1st, 2012, 04:01 PM
#7
Re: How do I make a console application continue running
What happens if I put a registry entry in that is bad - will I mess up this "remote" server I'm RDP'ed into and make it unbootable? Should I be afraid of changing my customers REGISTRY on their brand-spanking new Windows SBS 2011 box?
Seems a windows service has the benefit of me specifying a un/pw credential for it - the examples I saw of the service showed how to get through the INSTALL and stuff - seemed nearly impossible to debug that code though - I guess I could test it all in a console-like app and then put that into the service.
This was supposed to be easy to get working on Friday morning - chased process.start credential issues all weekend - and now it's at the end of Sunday and I'm still looking for solutions 
Thanks for your help!
-
Apr 1st, 2012, 04:03 PM
#8
Re: How do I make a console application continue running
Cross-post.
 Originally Posted by Joacim Andersson
Writing a value to the Run key in the register will only run your application if a user is logged on the server. I think you should go with the Windows Service route.
I have a couple of blog posts on this subject and one shows how you can run your windows service as a console application for debugging purposes.
https://msmvps.com/blogs/joacim/arch...e/default.aspx
Thanks for bringing me back down to ground!
I'll go the windows service route - the code is way simple that I'm executing anyway - and the service just stays running - right? No doevents or loops or anything else - right?
btw - do I need to setup the eventlog that I've seen in this example?
http://www.youtube.com/watch?v=a2J_XZW2S4I
Doesn't seem all that difficult - but wondering how bare-bones I can make this.
-
Apr 1st, 2012, 04:17 PM
#9
Re: How do I make a console application continue running
You don't have to set up the event log unless you want to log something. Now depending on what the BAT file is doing you must setup the right permissions for the service. The LocalSystem account has the highest permissions.
A windows service has a constructor but you should normally put your init code in the OnStart event. When you've written the code right click on the service designer and select Add Installer to add an Installer designer with a ServiceInstaller and ServiceProcessInstaller component. Use the properties of these component to setup the account, how the service should start, information text and things like that. You then have to use the InstallUtil.exe command line utility to actually install the service.
The blog post I linked to actually explains this in detail.
-
Apr 2nd, 2012, 04:18 PM
#10
Re: How do I make a console application continue running
Thank you very much for the help - this WINDOWS SERVICE is doing exactly what I want.
I tried to make my own log - but it's going into the APPLICATION LOG anyway - no big deal - I will probably kill logging after a week or two of this running nicely...
Code:
Public Class svcWinawcWatcher
Private Const EvtLogSource As String = "awcWatcher"
Private Const EvtLogName As String = "awcLog"
Private fsw As System.IO.FileSystemWatcher
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
If Not System.Diagnostics.EventLog.SourceExists(EvtLogSource) Then
System.Diagnostics.EventLog.CreateEventSource(EvtLogSource, EvtLogName)
End If
EventLog1.Source = EvtLogSource
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog1.WriteEntry("In OnStart")
Dim strWF As String = My.Settings.Item("WatchFolder")
fsw = New System.IO.FileSystemWatcher(strWF)
AddHandler fsw.Changed, AddressOf fsw_Changed
fsw.EnableRaisingEvents = True
EventLog1.WriteEntry("FileSystemWatcher enabled")
End Sub
Private Sub fsw_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
If e.Name.ToLower = "busy.txt" Then
Using fileRdr = New System.IO.StreamReader(e.FullPath)
Dim rptid As String = fileRdr.ReadLine
fileRdr.Close()
Try
Dim strFile As String = "Report_" & rptid & ".bat"
EventLog1.WriteEntry(strFile)
Dim parentFolder As String = System.IO.Directory.GetParent(System.IO.Path.GetDirectoryName(e.FullPath)).ToString
Dim strBatFile As String = System.IO.Path.Combine(parentFolder, strFile)
Process.Start(strBatFile)
Catch ex As Exception
EventLog1.WriteEntry(ex.Message)
End Try
End Using
End If
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
EventLog1.WriteEntry("In OnStop")
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|