How to do Multi-Threading

M

Michael D Murphy

Hi,
I need to use Windows Meadia Player control in my application to run som
mpeg movies. There is no user interface, I need to control everything
programatically. I do not want to use the playlist facility of the Windows
Media Player Control, I want to queue up the files to play myself. In order
to do that I need to know when the current file has been played and the
player has stopped.
I am looking at multi-threading as a solution, but am having difficultly
with the algorithm. I get the main thread to start the first clip and get
the worker function to check when the player has stopped the first clip, but
then I am wondering where I put the code to load the next mpeg file and
start the worker function again. Below is some snipits of code. Any help
would be appreciated.
Michael Murphy
(e-mail address removed)
954-452-1047

Imports System.Threading

Public Class frmMain

Inherits System.Windows.Forms.Form

Dim startworker As ThreadStart

Dim workerThread As Thread

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

startworker = New ThreadStart(AddressOf workerfunction)

workerThread = New Thread(startworker)

'DoCreate()

workerThread.Start()

End Sub

Public Sub DoCreate()

AxWindowsMediaPlayer1.uiMode = "none" ' no user interface, just the
player

If AxWindowsMediaPlayer1.URL = "" Then

AxWindowsMediaPlayer1.URL = GetNextAdvertisementFileName("Started")
' first time through

Else

AxWindowsMediaPlayer1.URL =
GetNextAdvertisementFileName(AxWindowsMediaPlayer1.URL) ' pass function the
last one played

End If

If workerThread.ThreadState = ThreadState.Stopped Then

workerThread.Start()

End If

End Sub

Private Sub workerfunction()

While (1)

If AxWindowsMediaPlayer1.playState <> WMPLib.WMPPlayState.wmppsStopped And
AxWindowsMediaPlayer1.playState <> WMPLib.WMPPlayState.wmppsUndefined Then

Thread.Sleep(700)

Else

Exit While

End If

End While

DoCreate()

End Sub

Private Function GetNextAdvertisementFileName(ByVal LastFileName As String)
As String

'stub out with these two mpeg files alternating for now

If LastFileName = "C:\Documents and Settings\MDMurphy\My Documents\Visual
Studio Projects\GasStationAdvert\ranier.mpg" Then

GetNextAdvertisementFileName = "C:\Documents and Settings\MDMurphy\My
Documents\Visual Studio Projects\GasStationAdvert\experience.mpg"

Else

GetNextAdvertisementFileName = "C:\Documents and Settings\MDMurphy\My
Documents\Visual Studio Projects\GasStationAdvert\ranier.mpg"

End If

End Function

End Class
 
P

Peter Huang

Hi Michael,

I think you may try to use the code to see if that is what you want.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.AxWindowsMediaPlayer1.URL = "c:\a.wma"
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
'handle the PlayStateChange event to be notified when the song has been
stopped
Private Sub AxWindowsMediaPlayer1_PlayStateChange(ByVal sender As
Object, ByVal e As AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles
AxWindowsMediaPlayer1.PlayStateChange
If e.newState = Me.AxWindowsMediaPlayer1.playState.wmppsStopped
Then
BeginInvoke(New MethodInvoker(AddressOf PlayIm))
End If
End Sub
Private Sub PlayIm()
Me.AxWindowsMediaPlayer1.URL = "c:\b.wma"
Me.AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
MsgBox(Me.AxWindowsMediaPlayer1.URL)
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor Ligthert

Peter,

I had the plan to do this, so thanks that you did it, I am as well curious
if this fit the problem.

Cor
 
M

Michael D Murphy

Peter,
Thanks for the help. I cut and pasted what I needed and with a few tweaks
here and there, it works great!
And Cor, thank you, too for following along on this.
We sure have a nice communitiy here, now don't we??
Michael
 
P

Peter Huang

Hi Michael,

I am glad the suggestion help you.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top