Results 1 to 10 of 10

Thread: [RESOLVED] How do I make a console application continue running

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: How do I make a console application continue running

    vb Code:
    1. Imports System.IO
    2.  
    3. Module awcWatcher
    4.  
    5.     Sub Main()
    6.  
    7.         Dim fsw As New FileSystemWatcher("D:\ACS Desktop\AWC\reporting\staging")
    8.  
    9.         fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
    10.  
    11.         AddHandler fsw.Changed, AddressOf fsw_Changed
    12.  
    13.         fsw.EnableRaisingEvents = True
    14.  
    15.         dim exit as string = console.readline
    16.         while exit.tolower <> "exit"
    17.             exit = console.readline
    18.         end while
    19.  
    20.     End Sub
    21.  
    22.     Private Sub fsw_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    23.         Debug.WriteLine(e.FullPath)
    24.     End Sub
    25.  
    26. End Module

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    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:
    1. Imports System.IO
    2.  
    3. Module Module1
    4.  
    5.     Public Sub main(ByVal args() As String)
    6.         Dim fsw As New FileSystemWatcher("D:\ACS Desktop\AWC\reporting\staging")
    7.  
    8.         fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
    9.  
    10.         AddHandler fsw.Changed, AddressOf fsw_Changed
    11.  
    12.         fsw.EnableRaisingEvents = True
    13.  
    14.     End Sub
    15.  
    16.     Private Sub fsw_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    17.         Debug.WriteLine(e.FullPath)
    18.     End Sub
    19.  
    20. 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

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    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:
    1. While True
    2.     Application.DoEvents()
    3. End While

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How do I make a console application continue running

    Cross-post.
    Quote Originally Posted by Joacim Andersson View Post
    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  10. #10

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width