I've just finished going through and threading my first section which is the music fade routines. I created a music fade thread object which calls the routines I had originally written, but this time I've Synclock'd the oDBMusic object so that every time the volume is changed between the two pices of music, no other thread can access the oDBMusic object. And to avoid any nasty multiple music changes happening at the same time, I created a musicLock object which is synclocked till the music fading has finished.
To show it in action visit this thread example website:
http://www.tyranntrpg.org/dgdk/thread_example/thread_example.html
[EDIT]
Here is the thread code to control the music fading:
Imports System.Threading
Public Class musicFadeThread
Private _thread As Thread
Private _fromID As String
Private _toID As String
Private _toLevel As Integer
Private _musicLock As Object
Public Sub New(ByVal ID As String, ByVal level As Integer, ByVal musicLock As Object)
_fromID = ID
_toLevel = level
_thread = New Thread(AddressOf workerFadeLevel)
_musicLock = musicLock
End Sub
Public Sub New(ByVal fromID As String, ByVal toID As String, ByVal musicLock As Object)
_fromID = fromID
_toID = toID
_thread = New Thread(AddressOf workerFromTo)
_musicLock = musicLock
End Sub
Public Sub New(ByVal ID As String, ByVal musicLock As Object)
_fromID = ID
_thread = New Thread(AddressOf workerFadeUp)
_musicLock = musicLock
End Sub
Private Sub workerFadeLevel()
SyncLock _musicLock
MusicFade(_fromID, _toLevel)
End SyncLock
End Sub
Private Sub workerFromTo()
SyncLock _musicLock
MusicFade(_fromID, _toID)
End SyncLock
End Sub
Private Sub workerFadeUp()
SyncLock _musicLock
MusicFadeUp(_fromID)
End SyncLock
End Sub
Public Sub Start()
_thread.Start()
End Sub
End Class
The code that sets up the threads and the music fade routines are:
Public Sub MusicSetVolume(ByVal ID As Integer, ByVal level As Integer)
SyncLock oDBMusic
oDBMusic.SetMusicVolume(ID, level)
End SyncLock
End Sub
Public Function LoopGDK() As Boolean
Dim result As Boolean
SyncLock oDBP
result = oDBP.LoopGDK
End SyncLock
Return result
End Function
Public Sub MusicFadeThreaded(ByVal fromID As String, ByVal toID As String)
Dim worker As New musicFadeThread(fromID, toID, _musicLock)
worker.Start()
End Sub
Public Function MusicFade(ByVal fromID As String, ByVal toID As String) As Boolean
If MusicCheck(fromID) = True And MusicCheck(toID) = True Then
Dim fromMusic As tyranntMusic
Dim toMusic As tyranntMusic
fromMusic = CType(mediaStore(fromID), tyranntMusic)
toMusic = CType(mediaStore(toID), tyranntMusic)
If MusicIsPlaying(fromMusic.MusicID) = False Or MusicIsPlaying(toMusic.MusicID) = True Then
Return False
End If
Dim level As Integer = MusicVolume(fromMusic.MusicID)
MusicSetVolume(toMusic.MusicID, 0)
MusicLoop(toID)
Dim lp As Integer = 0
While LoopGDK()
MusicSetVolume(fromMusic.MusicID, level - lp)
MusicSetVolume(toMusic.MusicID, lp)
Wait(10)
lp += 1
If lp > level Then
Exit While
End If
End While
Return True
Else
Return False
End If
End Function
Jas
----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"