INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Event Handling in VB.Net Tips  (Read 8994 times)

Smilin_Jim

  • Recent member
  • *
  • Posts: 33
Event Handling in VB.Net Tips
« on: December 01, 2007, 08:29:53 am »

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.

 :)
Logged

John Gateley

  • Citizen of the Universe
  • *****
  • Posts: 4957
  • Nice haircut
Re: Event Handling in VB.Net Tips
« Reply #1 on: December 01, 2007, 08:39:58 am »

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

  • Recent member
  • *
  • Posts: 33
Re: Event Handling in VB.Net Tips
« Reply #2 on: December 01, 2007, 08:59:08 am »

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.
Logged

bernardjp

  • Member
  • *
  • Posts: 1
Re: Event Handling in VB.Net Tips
« Reply #3 on: March 29, 2023, 12:46:17 pm »

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

Chriswires

  • Recent member
  • *
  • Posts: 8
Re: Event Handling in VB.Net Tips
« Reply #4 on: May 31, 2023, 12:44:12 am »

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!



Logged

JimH

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 71370
  • Where did I put my teeth?
Re: Event Handling in VB.Net Tips
« Reply #5 on: May 31, 2023, 06:00:33 am »

You might have better luck with the more current MCWS interface here:   https://wiki.jriver.com/index.php/DevZone
Logged

eve

  • Citizen of the Universe
  • *****
  • Posts: 651
Re: Event Handling in VB.Net Tips
« Reply #6 on: May 31, 2023, 12:48:49 pm »

You might have better luck with the more current MCWS interface here:   https://wiki.jriver.com/index.php/DevZone

Jim. I know this is a little off topic but is there any chance of getting JRiver to 'push' data / it's state, rather than requiring polling? Something like MQTT would be extremely helpful for specific functions such as what ChrisWires is trying to accomplish " implement events such as when new track is initiated in a playlist"
Since it very much seems that MCWS is your guys preferred way to interact with JRiver (which makes sense), I'd love to see it's utility expanded.
Logged
Pages: [1]   Go Up