Hi,
I have code in a COM Dll plug-in that trucks out MIDI sysex to a midi device.
Because of the large size of the sysex dump and the time it takes to execute I created a thread to do the work. Historically I was using _beginthread to get the thread going but recently found it necessary to know when the thread was finished so I switched over to _beginthreadex as it can return a handle to the started thread that WaitForSingleObject can use.
The main code stuffed 4 32 word arrays with data that the thread will ultimately brute force change the bytes in a global array that is more or less a sysex dump "template" that the thread will modify with the bytes in the arrays.
The Actual midi transmission of the data is what takes the most time. About a 1/2 second for each call to myMidiOut->MidiOutLongMessage. At such time I am at the mercy of the host application "Sonar" as it sends the sysex midi string out.
When I switched from _beginthread (which worked), to _beginthreadex fun began.
The thread is started like this:
HANDLE hThread;
hThread = (HANDLE)_beginthreadex( NULL, 0,
CBCRMaster::SetKnobLed, m_pMidiOut, 0, NULL);
hThread =(HANDLE) _beginthread(CBCRMaster::SetKnobLed,0,m_pMidiOut);
switch (WaitForSingleObject(hThread, INFINITE))
{
case WAIT_OBJECT_0 :
{
break;
}
case WAIT_TIMEOUT :
{
TerminateThread(hThread, 0);
break;
}
default :
{
TerminateThread(hThread, 0);
break;
}
}
The Thread Code: The data arrays are stuffed by the main code. WorkLoad is the amount of data stuffed in the arrays.
unsigned int WINAPI CBCRMaster::SetKnobLed(void * m_pMidiOut)
{
ISonarMidiOut * myMidiOut;
myMidiOut = (ISonarMidiOut*)m_pMidiOut;
while(WorkLoad > -1)
{
myMidiOut->MidiOutLongMsg((IControlSurface*)SurfaceAddress[0],sizeof(Sysex_SetEncoder),Sysex_SetEncoder);
WorkLoad--;
}
return 0;
}
Now the problem:
If the switch (WaitForSingleObject(hThread, INFINITE)) code is in place, when the thread gets to the myMidiOut->MidiOutLongMessage code it never then will execute another line of code, the debugger just acts as if the thread went into the weeds.
The Sysex dump is never sent to the receiving device either.
If I comment out the entire switch (WaitForSingleObject(hThread, INFINITE)) block and just keep on trucking, the Thread executes as it should but I never get to know when it is finished.
However... If I leave the (WaitForSingleObject(hThread, INFINITE)) block in to execute, but comment out the myMidiOut->MidiOutLongMessage line then the thread runs as it should and furthermore the WaitForSingleObject event fires with WAIT_OBJECT_0.
I have tied increasing the stack from 0 to 30 with no avail and I should reiterate that the whole shootin' match worked with _beginthread.
Thanks.
:Ron