-
Feb 20th, 2012, 02:49 PM
#1
Thread Starter
Lively Member
How do I use Event Handlers
Hi all I'm am trying to do sum custom events in a Class. My class listens for a UDP Broadcast, I want to Raise and Event when a message is received to display the message from a windows form. I am having no luck. Can sum one help.
Here is my code.
Code:
Public Class UDP_Broadcast
Public Event NewMessage(ByVal Message As String)
Public Port As Integer = 2456
Public Sub Receiver_Load()
Dim t As New Threading.Thread(AddressOf listen)
t.IsBackground = True
t.Start()
End Sub
Public Sub Receiver_Closing()
If udp IsNot Nothing Then udp.Close()
End Sub
Private udp As Net.Sockets.UdpClient
Private Sub listen()
Try
udp = New Net.Sockets.UdpClient(Port)
udp.EnableBroadcast = True
Dim ep As New Net.IPEndPoint(Net.IPAddress.Broadcast, Port)
Do
Dim b() As Byte = udp.Receive(ep)
'Me.Invoke(safeAddText, System.Text.Encoding.UTF32.GetString(b))
safeAddText(System.Text.Encoding.UTF32.GetString(b))
Loop
Catch ex As Exception
If udp IsNot Nothing Then udp.Close()
End Try
End Sub
Private Delegate Sub delAddText(ByVal text As String)
Private safeAddText As New delAddText(AddressOf AddText)
Private Sub AddText(ByVal text As String)
RaiseEvent NewMessage(text)
'MsgBox(text)
End Sub
End Class
Form Code
Code:
Private WithEvents Mess As UDP_Broadcast
Private Sub UDP_Broadcast1(New_UDP_Mess As String) Handles Mess.NewMessage
MsgBox(New_UDP_Mess)
End Sub
Last edited by Brian Henry; Feb 20th, 2012 at 03:50 PM.
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Feb 20th, 2012, 04:07 PM
#2
Re: How do I use Event Handlers
do you ever get past Dim b() As Byte = udp.Receive(ep)????
-
Feb 20th, 2012, 04:08 PM
#3
Re: How do I use Event Handlers
You have to define an event in your class
Code:
Public Event EventName(ArgumentList)
Then you have to raise the event somewhere
Code:
RaiseEvent EventName(ArgumentList)
In your main form or wherever it is to be used you would dim it with events as you have and the have a sub routine that handles the event.
looks like you are only missing the event definition.
-
Feb 20th, 2012, 04:10 PM
#4
Re: How do I use Event Handlers
Never mind, when I looked at the code the line where the event was defined had scrolled off the top and I did not see it.
-
Feb 20th, 2012, 05:09 PM
#5
Re: How do I use Event Handlers
If you raise that event from a background thread, you'll be raising it on the wrong thread.
Over in the .NET CodeBank I have a UDP class that is derived from a class I use in a robotics project (which can be found in the Networking forum in a thread I started). It deals with broadcasts, too, so it might be of iterest for a variety of reasons.
My usual boring signature: Nothing
-
Feb 20th, 2012, 05:52 PM
#6
Thread Starter
Lively Member
Re: How do I use Event Handlers
Thanks I will check it out.
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Feb 20th, 2012, 06:16 PM
#7
Re: How do I use Event Handlers
Originally Posted by Brian Henry
I am having no luck.
In what way is it not working? What do you expect to happen, and what actually happens?
Originally Posted by Shaggy Hiker
If you raise that event from a background thread, you'll be raising it on the wrong thread.
True dat. You don't want to be messing around with Invokes in the form code if you can avoid it. You can make sure events are raised on the thread that created the UDP_Broadcast instance (presumably it is created on the UI thread?) by taking a reference to the SynchronizationContext.Current property in the constructor (add a Public Sub New() method). This will give you a SynchronixationContext instance that you can call Post or Send from the secondary thread to execute code on the first thread. These two methods are analogous to BeginInvoke and Invoke, respectively.
-
Feb 20th, 2012, 07:59 PM
#8
Re: How do I use Event Handlers
That's the exact technique I used for posting events to the UI thread in the class I referenced. I believe JMC has also suggested a different method that might be easier to use, but it can't be MUCH easier, because SynchronizationContext is about as simple as anything can be.
My usual boring signature: Nothing
-
Feb 22nd, 2012, 07:45 PM
#9
Thread Starter
Lively Member
Re: How do I use Event Handlers
Thanks for the help, I got it to work.
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Feb 22nd, 2012, 11:26 PM
#10
Re: How do I use Event Handlers
Originally Posted by Shaggy Hiker
That's the exact technique I used for posting events to the UI thread in the class I referenced. I believe JMC has also suggested a different method that might be easier to use, but it can't be MUCH easier, because SynchronizationContext is about as simple as anything can be.
There's basically only three ways to do it:
1. In Windows Forms, use the Invoke or BeginInvoke method of a Control.
2. In WPF, use the Invoke or BeginInvoke method of a Dispatcher.
3. Use the Send or Post method of a SynchronizationContext.
The SynchronizationContext class is actually inherited by the WindowsFormsSynchronizationContext and DispatcherSynchronizationContext classes and it is an instance of one of those that SynchronizationContext.Current returns. Those derived classes actually use options 1 and 2 under the hood anyway, which is why SynchronizationContext is only of use in a Windows Forms or WPF app. I believe that the Current property is always Nothing in any other type of app.
-
Feb 22nd, 2012, 11:39 PM
#11
Re: How do I use Event Handlers
In contexts other than a Windows Forms or WPF app, you would be expected to supply your own (or the framework to supply) a derivation of SynchronizationContext. You can set the SyncronizationContext of a thread manually (WinForms and WPF just do this for you in their start-up code).
For instance, you could unit test the class discussed in this thread by supplying a test-double SynchronizationContext. (Actually, aside from the tests for the thread invoking behaviour, you'd probably just use the SynchronizationContext implementation directly)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|