Hello,
I'm a bit stuck here...
Background:
a program that (eventually) will save data synchronised from a MTC. (ive worked out all the mtc stuff)
The code I am using uses winmm.dll to receive midi messages. This works fine, I can process incoming packets etc.
using vb2010
What I'm stuck with:
The callback function when a packet is received can't output anything on my form. I'm aware this is most likely a threading issue, and I have tried many ways of getting around this, and I cant figure it out. The latest is detailed below
Code:
Midi Module:
Module Module1
'all the midi declarations
Delegate Function MidiIn_Callback(ByVal hMidiIn As Integer, ByVal wMsg As UInteger, ByVal dwInstance As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
Public Declare Function midiInOpen Lib "winmm.dll" (ByRef lphMidiIn As Integer, ByVal uDeviceID As Integer, ByVal dwCallback As MidiIn_Callback, ByVal dwInstance As Integer, ByVal dwFlags As Integer) As Integer
Public Declare Function midiInClose Lib "winmm.dll" (ByVal hMidiIn As Integer) As Integer
Public Declare Function midiInStart Lib "winmm.dll" (ByVal hMidiIn As Integer) As Integer
Public hMidiIn As Integer
Public Const CALLBACK_FUNCTION As Integer = &H30000
'these are public for debug purposes.
Public frames As Int16
Public seconds As Int16
Public minutes As Int16
Public hours As Int16
Public timecode As Long
Public byte1 As Byte
Public byte2 As Byte
Public nibble1 As Byte
Public nibble2 As Byte
Function MidiInProc(ByVal hMidiIn As Integer, ByVal wMsg As UInteger, ByVal dwInstance As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
'MTC is 2 bytes. Byte 1 is F1 (means its an MTC packet). Byte 2 is a frame counter & frame value
byte1 = dwParam1 And &HFF
'skip if it isnt a frame packet
If byte1 <> &HF1 Then
Return 0
Exit Function
End If
'byte 2 is split into nibbles. MS Nibble is Message Type, LS Nibble is Message
byte2 = dwParam1 >> 8
nibble1 = byte2 And &HF
nibble2 = byte2 >> 4
'operate!
Select Case nibble2
Case 0 'frame lsb
'mask
frames = (frames And &HF0) Or nibble1
Case 1 'frame msb
'shift
nibble1 = nibble1 << 4
'apply
frames = (frames And &HF) Or nibble1
Case 2 'second lsb
'apply
seconds = (seconds And &HF0) Or nibble1
Case 3 'second msb
'shift
nibble1 = nibble1 << 4
'apply
seconds = (seconds And &HF) Or nibble1
Case 4 'minute lsb
'apply
minutes = (minutes And &HF0) Or nibble1
frames = frames + 1
Case 5 'second msb
'shift
nibble1 = nibble1 << 4
'apply
minutes = (minutes And &HF) Or nibble1
Case 6 'hours lsb
'apply
hours = (hours And &HF0) Or nibble1
Case 7 'hours msb
'shift
nibble1 = nibble1 << 4
'apply
hours = (hours And &HF) Or (nibble1 And &H1)
End Select
'------------------ this call works, and passes the argument. debug.print(arg0) shows this, see below
If nibble2 = 1 Or nibble2 = 5 Then
Form1.SomeSub(hours & ":" & minutes & ":" & seconds & ":" & frames & " -- " & nibble2)
End If
End Function
End Module
Form Module:
Private Delegate Sub DelegateForSomeSub(ByVal arg0 As String)
Public Sub SomeSub(ByVal arg0 As String)
If Me.InvokeRequired Then
' Argument array for the delegate
Dim args As Object() = {arg0}
' Method to call
Dim SomeSubDelegate As DelegateForSomeSub
SomeSubDelegate = AddressOf SomeSub
' After this you're in the main thread (UI thread)
Me.Invoke(SomeSubDelegate, args)
Exit Sub
End If
'this doesnt work
TextBox1.AppendText(arg0)
'this does work
Debug.Print(arg0)
End Sub
the delegate sub (etc) is lifted straight from this post -> http://www.daniweb.com/software-development/vbnet/threads/293937
I'd really like to understand why this isnt working. I've trawled the internet (no doubt someone will find a link i didnt), and I cant find anything that a)works or b)i can make work
Anyone have any ideas?