I am porting an existing VB .NET application into a console (no-gui) application so that it may be run without any user input other than command line parameters.
In doing so I do not seem to get any Event handling mechanism. Is this framework only provided by the Form library
For example, I am using a MSComm control for serial port access and I want to handle the OnComm event. When running my console application I do not see any events...
Any help would be much appreciated. Thanks in advance.
Jay
2003 VB .NET Standard

Console Applications and Event Handling?
Jose Coig
UGH,
I can't go too far in testing your code because I have a very latter day system and I don't even know it the com ports are enabled and I don't have anything connected to them or to test with.
Several notes. I'm running the Team Suite RC1. It's almost the the release version and I do see the event in the IDE. I can't tell if it will fire or not because although I have the hardware I don't even think it's enabled and I'd really be afraid to execute this program.
No matter what I cannot get the timer to define with either system.timers.timer or system.threading.timer.
Next issye is that I bet you are runing with explicit off because m_ModemPort is not defined.
At least from here it looks as if that variable is left to chance. I'd turn option Explicit on and leave it on.
I wish I caould help more. What version of VB are you running
Denise
I played with this a bit and on the surface it appears that you are right, there was no sign of events.
I played some more and added a class file and instantiated the class file in the console task module. The two files looked like this:
File module1.vb
Module Module1
Dim a As New NetClass
Sub Main()
a.foo()
End Sub
End Module
File class1.vb
Public Class NetClass
Inherits System.Windows.Forms.Form
Public Function foo() As String
foo = ""
MsgBox("hello", MsgBoxStyle.Critical, "")
End Function
End Class
I also made sure that the class had .NET references to System.Windows.Forms
The class had full access to forms events as displayed in the IDE. It appears, thats the way to go. I did excute this program and the messagebox did popup, however under the console window.
You probably will not see events in a module, but you can see them in a class called by the module:
{Help militate for icons with hair!!!}
John Bevington
Ok, I played a little more.
I made sure to add a reference to MSCOMM32.OCX
And I modified the class just a bit:
Imports System.net
Public Class NetClass
Inherits System.Windows.Forms.Form
Friend WithEvents a As New MSCommLib.MSComm
Public Function foo() As String
foo = ""
MsgBox("hello", MsgBoxStyle.Critical, "")
End Function
Private Sub a_OnComm() Handles a.OnComm
End Sub
End Class
I was able to see the event and the IDE declared the OnComm event above. Then i tried something else. I deleted the class and tried the module only like so:
Module
Module1
Friend WithEvents a As New MSCommLib.MSComm
Sub Main() End Sub Private Sub a_OnComm() Handles a.OnComm End SubEnd
ModuleAfter "a" was declared as a Friend WithEvents the Ide saw the OnComm() event and was able to add the event to the module itself.
js40
System.Windows.Forms.Application.DoEvents()
I now receive events.
Thanks for the assistance.
TheBlackhorse
Here is some updates for the example you started. In my case I never see the OnComm Event. What am I missing
Module Module1
Dim Done As Boolean = False
Friend WithEvents a As New MSCommLib.MSComm
Sub Main()
' Setup a timer to generate a comm event
Dim Timer1 As New Timer(New TimerCallback(AddressOf CommEventGen), Nothing, 1000, 0)
Console.WriteLine("Wait on Done")
While Not Done
' Do something in the meantime
System.Threading.Thread.Sleep(1000)
Console.WriteLine("Waiting on Done")
End While
Console.WriteLine("Done")
End Sub
Private Sub a_OnComm() Handles a.OnComm
Console.WriteLine("OnComm Event, Done = True")
Done = True
End Sub
Sub CommEventGen(ByVal state As [Object])
Console.WriteLine("CommEventGen")
a.CommPort = m_ModemPort 'control is using COM1
a.Settings = "38400,n,8,1" 'set up the port parameters
a.RThreshold = 1 'set the oncomm event to trigger whenever data arrives
a.SThreshold = 1 'set the oncomm event to trigger whenever data arrives
a.PortOpen = True 'open the port
' Send out the serial port which will cause oncomm event to fire
a.Output = "buffer to generate CommEvent"
End Sub
End Module
Thanks for the prompt reply!