Results 1 to 11 of 11

Thread: How do I use Event Handlers

  1. #1

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    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

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How do I use Event Handlers

    do you ever get past Dim b() As Byte = udp.Receive(ep)????

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  6. #6

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    Re: How do I use Event Handlers

    Thanks I will check it out.
    Brian Henry
    Visual Studio 2001 to 2019
    Java/Android
    ISaGRAF

  7. #7
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: How do I use Event Handlers

    Quote Originally Posted by Brian Henry View Post
    I am having no luck.
    In what way is it not working? What do you expect to happen, and what actually happens?

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  9. #9

    Thread Starter
    Lively Member Brian Henry's Avatar
    Join Date
    Oct 2005
    Posts
    94

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: How do I use Event Handlers

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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
  •  



Click Here to Expand Forum to Full Width