Link to home
Start Free TrialLog in
Avatar of LiebertUser
LiebertUser

asked on

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

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

Avatar of Wayne Michael
Wayne Michael
Flag of United States of America image



use a do while loop and put in a thread.sleep and a doevents.
do events is only valid with forms..

so this should work.

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

Open in new window

you want a way to trigger a terminate though?
have you considered a service?

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

Avatar of LiebertUser
LiebertUser

ASKER

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.
you can use console.readkey to see if any keystroke happened.

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

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?
you may not even need to the sleep with you do a read key..
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.




I suggest to put sleep(1000) inside a while loop so you will have better responsivity
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

egl1044:  how would that terminate?
Avatar of Mike Tomlinson
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.
Idle_Mind,
This is a little embarressing but how do I inherit a class from applicationcontext and pass that instance to application.run()...
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

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
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.  =(
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial