Windows > Plug-in Development

Event Handling in VB.Net Tips

(1/2) > >>

Smilin_Jim:
I just finished working on an app in Visual Basic 2005 Express (an update to BusyBox) and realized that the documentation on event handling in VB.Net is incomplete everywhere that I looked.  As far as it goes, the existing Wiki entry is correct, but I couldn't implement that into working code. 
The problem is that the wiki example just raises a message box.  If you try to actually modify anything in your base class (e.g. - Form1) from the subroutine then you get exception errors.

The problem is that the event handler in VB2k5 runs in a separate thread and only the thread that the class object is running in can update its children. (Non-C programmer interpretation.)

There is a method to make it work.  It involves a class called a delegate which contains a pointer for the event that can be used by the main thread.  In otherwords, the delegate can look at the event handler thread and update the main program when there is a change.

The following code performs this function.

In the header, the Media Center object is referenced with the WithEvents modifier as in the original example.  The delegate creating subroutine RelayEvent and its instance _RelayEvent are created with the three string parameters for the MediaCenter event.

Imports MediaCenter
Public Class MainForm
    Dim WithEvents MC As MediaCenter.MCAutomation
    Private _RelayEvent As RelayEvent
    Delegate Sub RelayEvent(ByVal EventData1 As String, ByVal EventData2 As String, ByVal EventData3 As String)
...


A subroutine is written in the form MainForm (or Form1 or whatever) that is activated by the event handler task (via the delgate RelayEvent) when it receives an event from the MediaCenter object/program.  This task is then used to update any information in the regular part of the program (main thread.)

Private Sub MyRelayEvent(ByVal EventData1 As String, ByVal EventData2 As String, ByVal EventData3 As String)
 
        lbParam1.Text = EventData1
        lbParam2.Text = Trim(EventData2)
        lbParam3.Text = EventData3
        Application.DoEvents()

    End Sub

The event handling task has to be modified slightly from the wiki example to tell it to use the delegate. To do this, in the event handler _RelayEvent is assigned as the delegate pointer (using the RelayEvent routine) referencing the subroutine MyRelayEvent over on the main thread.  Then the BeginInvoke method is used to call MyRelayEvent via the delegate pointer.

Public Sub MC_FireMJEvent(ByVal s0 As String, ByVal s1 As String, ByVal s2 As String) Handles MC.FireMJEvent

        _RelayEvent = New RelayEvent(AddressOf MyRelayEvent)
        BeginInvoke(_RelayEvent, s0, s1, s2)

    End Sub

This will allow events to correctly update items in VB.Net.  As an aside, the same MediaCenter object, MC, can be used for all of the other method and property calls.  You do not need to create a separate one for accessing the other stuff in your program.

 :)

John Gateley:
Thanks VERY much. Could you add this to the wiki? Doesn't matter if it's perfect, just getting the info there would be great.

j

Smilin_Jim:
I will update the wiki, but I wanted to give it a couple of days for some more veteran programmers to review it and make sure that I was using the right words to describe what's going on.
I also incorporate this logic in an updated BusyBox.  This should be coming along shortly, too.

bernardjp:
Hi,
Do you have a code with the same functionality for VBA?

Chriswires:
Hi. Thanks for the revision to events.

I too have been looking at the guides to implement events such as when new track is initiated in a playlist and there seems to be an assumption regarding user knowlege.
 
I have visual studio 2022. I have added the reference to media centre. I am using visual basic. NET but if I use any of the code from the wiki I get many debug errors. If I start my own simple code to test the linkage to media centre automation. I get null data responses.

Clearly I am missing something to do with the platform / environment that the code is expected to be run on. But I suspect I am not alone in this.

Any help would be gratefully received!



Navigation

[0] Message Index

[#] Next page

Go to full version