Hey everybody,
I've got what seems to be a simple problem, but I just can't get my head around it.
I have narrowed it down to this:
When I use
mciSendString
to open, play and pause a wav file, the play/pause commands only work when being sent from the very same thread the 'open' command was called.
So this works:
mciSendString("open ""C:\file.wav"" type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero)
mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero)
thread.sleep(300)
mciSendString("pause audiofile", Nothing, 0, IntPtr.Zero)
thread.sleep(300)
mciSendString("resume audiofile", Nothing, 0, IntPtr.Zero)
thread.sleep(300)
mciSendString("close audiofile", Nothing, 0, IntPtr.Zero)
But this does not:
dim t1 as thread, t2 as thread
t1 = new Thread(addressof openFile)
t1.start()
t2 = new Thread(addressof playFile)
t2.start()
'etc.
private sub openFile()
mciSendString("open """ & fileName & """ type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero)
end sub
private sub playFile()
mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero)
end sub
'etc.
This is, of course, not the actual code, but you get the idea.
When I debug it with
console.writeline(Thread.CurrentThread.ManagedThreadId)
, I can see that in those cases the threads have the same ThreadId, the commands succeed (return value of mciSendString is zero), and in those cases the mciSendString commands get called by threads with different ThreadIds, they fail (Return value is 263 -- INVALID_DEVICE_NAME).
I googled my brain out, but I cannot find an example of how to create a thread that has a constant ThreadId, or how to simply invoke a method in a certain thread.
I might be blinded by the whole lotta code I read and tried out, but right now it seems I can't get to the answer.
If somebody could point me to the right direction, that would be super cool.