Function call only works when MessageBox.Show() is included?

The PlayStateChanged event is quite notorious. It was really meant to just update a UI element that shows the state. Doing anything with the player in that event is very troublesome. A call to MessagBox can have an affect because it pumps a message loop, always a big deal for ActiveX controls.

The best way to stay out of trouble is by delaying your code, making it run after the event was fired and the player is back into a quiescent state. Elegantly done by using the Control.BeginInvoke() method. Like this:

Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As System.Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles AxWindowsMediaPlayer1.PlayStateChange
    If e.newState = WMPLib.WMPPlayState.wmppsStopped Then
        Me.BeginInvoke(New Action(AddressOf NextSong))
    End If
End Sub

Private Sub NextSong()
    musictimer("next")
End Sub

Leave a Comment