.NET Events Outside of Forms

robdog09

New member
Joined
May 15, 2006
Messages
1
Programming Experience
5-10
Specifically, I have a console application that im working with, but i think this topic applies to more than console apps.

The problem is i have an API that interacts with an external device on my computer. Not really relevant what the device is but the device raises events, which are it turn sent through the api where i can handle them back in code. I have a Windows Form where i declare the a new instance of the api WITHEVENTS and it handles them correctly. I want to convert this form application to a console application, eventually going to a service, but a console app for now. But when i declare an instance of the API WITHEVENTS in the console app, once processing is done in the module, the console app just exits and does not wait for events. How can i simulate a windows form in the console app enviornment to wait for events and just not exit?
 
Console application isn't particulary suited for events, but it can be done by starting the event listener as a separate thread and looping it until cancelled by main thread where Console would block by waiting for user input (press any key). Example:
VB.NET:
[SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE][SIZE=2] Module1[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Main()[/SIZE]
[SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] ev [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] eventedClass[/SIZE]
[SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] t [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Threading.Thread([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] ev.watcherThread)[/SIZE]
[SIZE=2] t.Start()[/SIZE]
[SIZE=2] Console.ReadKey([/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] ev.cancel = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2] t.Join()[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Module[/COLOR][/SIZE]
[/COLOR][/SIZE]
VB.NET:
[SIZE=2][COLOR=#0000ff]Friend [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] eventedClass[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] cancel [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][SIZE=2] time [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Timers.Timer(1000)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] watcherThread()[/SIZE]
[SIZE=2] Console.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Waiting for an event..."[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2] time.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] Do [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Until [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cancel = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]  Threading.Thread.Sleep(1000)[/SIZE]
[SIZE=2][COLOR=#0000ff] Loop[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] time_Elapsed([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Timers.ElapsedEventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] time.Elapsed[/SIZE]
[SIZE=2] Console.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"timer event occurred."[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 
Back
Top