How do I keep a console VB application open and responding to object events after Sub Main has finished?

LiebertUser
LiebertUser used Ask the Experts™
on
I'm writing my first console application in VB 2005.  Fortunately, its also likely to be the simplest application that I've ever created.
I want to watch a folder using the .NET filesystemwatcher.  My question is, how do I keep the program 'alive'?  See my extremely simple example below.  This is just the skeleton but the complete app won't do much more than just react to the FileSystemWatcher_Created event and copy a file.
When I run the program, it prints 'Hello World' and instantly closes.  Fair enough - but I just want it to spin its wheels and let the Filesystemwatcher and its event do all the work.  For my forms based apps this has never been an issue because as long as I have a form, my app is open.
I guess I don't understand the life and scope of objects very well.  I mean, if a form object has enough sense to stay open, why won't a filesystemwatcher object stay open?
Thanks

Imports System.IO
Module Module1
    Dim WithEvents FSW As New FileSystemWatcher("O:\FTP\clp1\lsfjr009", "*.txt")
    Sub Main()
               Console.WriteLine("Hello World")
    End Sub
    Private Sub FSW_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSW.Created
        Debug.Print("test success!" & Now.ToString)
    End Sub
End Module

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Wayne MichaelSenior Software Developer

Commented:


use a do while loop and put in a thread.sleep and a doevents.
Wayne MichaelSenior Software Developer

Commented:
do events is only valid with forms..

so this should work.

 Do While True
            System.Threading.Thread.Sleep(32000)
 
        Loop

Open in new window

Wayne MichaelSenior Software Developer

Commented:
you want a way to trigger a terminate though?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Wayne MichaelSenior Software Developer

Commented:
have you considered a service?

Wayne MichaelSenior Software Developer

Commented:
Added System.Windows.Forms

Good luck.


Imports System.Windows.Forms
 
Module Module1
 
    Sub Main()
 
 
        Do While True
            System.Threading.Thread.Sleep(32000)
            Application.DoEvents()
        Loop
    End Sub
 
End Module

Open in new window

Author

Commented:
wrmichael, yes, I will want to be able to terminate it.  Just pressing <ENTER> and having it say 'Goodbye World' would be perfect for my learning example.

I've written one service with good results and agree with you that a service would be suitable except for one little 'GOTCHA'.  The drive share I'm monitoring is a secured network drive so someone would need to be logged in, with permission to the network share.  A service might come later, but right now I don't have a problem with having it run under a user's login.
Wayne MichaelSenior Software Developer

Commented:
you can use console.readkey to see if any keystroke happened.

if so you can break the loop.  (exit do)

Author

Commented:
wrMichael,  I know I just need to try this but your answers are coming pretty fast so I thought I would ask while your at your keyboard.  
If I do the sleep ((32000) = 32 seconds I think) will my event still fire while the sleep thread is running?  Or will my event be queued and then run when the Doevents executes?
Wayne MichaelSenior Software Developer

Commented:
you may not even need to the sleep with you do a read key..
Wayne MichaelSenior Software Developer

Commented:
Run your other code that is doing to watch in another thread,  then the sleep will not matter,  but you may not need the sleep at all or the doevents.




Jaime OlivaresSoftware Architect
Top Expert 2008

Commented:
I suggest to put sleep(1000) inside a while loop so you will have better responsivity
Top Expert 2010

Commented:
Try this
Imports System.IO
 
Module Module1
 
    Dim fsw As New FileSystemWatcher("D:\test", "*.txt")
 
 
    Public Sub SyncWaiter()
 
        Dim e = fsw.WaitForChanged(WatcherChangeTypes.All, -1)
        If e.ChangeType = WatcherChangeTypes.Created Then
            Console.WriteLine(e.Name)
        End If
        SyncWaiter()
 
    End Sub
 
    Sub Main()
 
        Console.Title = "Watcher"
        Console.WriteLine("Waiting for a file to be created...")
        SyncWaiter()
 
 
    End Sub
 
End Module

Open in new window

Wayne MichaelSenior Software Developer

Commented:
egl1044:  how would that terminate?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I would suggest you not use a Console app...  =\

If you want a formless app start with a WinForms project.  Uncheck the "Enable Application Framework" box in Project --> Properties and add a Module with a Sub Main in it.  Then Inherit a Class from ApplicationContext and pass your instance of that to Application.Run() in Sub Main().  This will allow you to have a formless app with a message pump (that can receive events) that doesn't die and doesn't have a klunky while loop.

Author

Commented:
Idle_Mind,
This is a little embarressing but how do I inherit a class from applicationcontext and pass that instance to application.run()...
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
No worries man...  =)

Add a Module with the code below.
Uncheck "Enable Application Framework" in Project --> Properties.
Change the "Startup Object" to "Sub Main".

Something like:
(you can event declare variables as "WithEvents" at the class level so you don't have to use AddHandler() to wire them up)
Module Module1
 
    Public Sub Main()
        Application.Run(New MyContext)
    End Sub
 
    Public Class MyContext
        Inherits ApplicationContext
 
        Public Sub New()
            ' *** This is the new "entry point" into your app! ***
        End Sub
 
        Public Sub Foo()
            ' To kill the app programatically:
            Application.Exit()
        End Sub
 
    End Class
 
End Module

Open in new window

Author

Commented:
Idle_Mind, that works in as much as it doesn't just quit running after going through sub main and my filesystemwatcher does respond to events but how do I get it to open a command window and write to that window (much like console.writeline does).
If I doubleclick the example app it just invisiblly starts even though I have a console.writeline statement.
If I start it from the command line the console.writeline still doesn't write anything (although it doesn't blow up so its writing to something).
Its my bosses requirement that this little app just be a command line app.  Any thoughts?  
Thanks
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Haha...well, you can't have it both ways.  =\

It's possible to spawn your OWN console window and write to that...

If it has to be a true console app then you'll have to make a polling loop as the others suggested.  =(
Top Expert 2010
Commented:
This should work for your console. The example is slightly modified from MSDN documents.
Imports System.Security.Permissions
Imports System.Diagnostics
Imports System.IO
 
 
Public Class Module1
 
    Public Shared Sub Main()
 
        Run()
 
    End Sub
 
    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
    Private Shared Sub Run()
 
        Dim args() As String = System.Environment.GetCommandLineArgs()
        If args.Length <> 2 Then
            Console.WriteLine("Usage: Watcher.exe (directory)")
            Return
        End If
 
        Dim watcher As New FileSystemWatcher()
 
        watcher.Path = args(1)
 
        watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
 
        watcher.Filter = "*.txt"
 
        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Created, AddressOf OnChanged
        AddHandler watcher.Deleted, AddressOf OnChanged
        AddHandler watcher.Renamed, AddressOf OnRenamed
 
        watcher.EnableRaisingEvents = True
 
        Console.WriteLine("Press 'q' to quit the sample.")
 
        While Chr(Console.Read()) <> "q"c
        End While
 
    End Sub
 
    Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        If e.ChangeType = WatcherChangeTypes.Created Then
            '// TODO:: work
            Console.WriteLine("File: " & e.FullPath)
        End If
    End Sub
 
    Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath)
    End Sub
 
End Class

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial