serial reader ui-thread solution?
Hi,
is it also possible to use an ui-thread for the serial reader-method?
I`ve two threads which "can" receive the receive message from the
serial reader-thread; but only one of these two threads will receive
the message from the serial-reader-thread. The user is able to switch
between these two threads.
Using a normal workerthread (for the serial reader thread), I always
have to close the thread (serial-reader) and re-create it, if the user
switch between the two threads receiving the message fromt he serial
reader.
Using a ui thread I can post message to the serial reader thread, that
the user wants that threadA will receive the pkts or threadB.
while(running)
{ /* read loop */
BYTE buffer[MAX_RXD_BUFFER_SIZE];
BOOL ok = ::ReadFile(parms->hCom, buffer, MAX_RXD_BUFFER_SIZE - 1,
&bytesRead, &ovl);
if(!ok)
{ /* error */
DWORD err = ::GetLastError();
if(err != ERROR_IO_PENDING)
{ /* read error */
shutdown = err;
::PostThreadMessage(parms->artnetTh, UWM_READER_SHUTTING_DOWN,
(WPARAM)parms->channelID, (LPARAM)err);
running = FALSE;
continue;
} /* read error */
// otherwise, it is ERROR_IO_PENDING
DWORD result = ::WaitForMultipleObjects(2, waiters, FALSE, INFINITE);
switch(result)
{ /* wait */
case WAIT_OBJECT_0: // shutdown
::CancelIo(parms->hCom);
shutdown = ERROR_SUCCESS; // clean shutdown
running = FALSE;
continue;
case WAIT_OBJECT_0 + 1: // I/O complete
ok = ::GetOverlappedResult(parms->hCom, &ovl, &bytesRead, TRUE);
if(!ok)
{ /* GetOverlappedResult failed */
DWORD err = ::GetLastError();
::PostThreadMessage(parms->artnetTh, UWM_READER_SHUTTING_DOWN,
(WPARAM)parms->channelID, (LPARAM)err);
running = FALSE;
continue;
} /* GetOverlappedResult failed */
break;
default:
{ /* trouble */
shutdown = ::GetLastError();
running = FALSE;
} /* trouble */
} /* wait */
}
//write this received data msg to threadA or threadB (determining of
threadID)
::PostThreadMessage(threadID, UWM_READER_SENDING_DATA,(WPARAM)pData,
0);
if the thread waits in the (DWORD result = ::WaitForMultipleObjects(2,
waiters, FALSE, INFINITE)) the whole thread is blocked so that I can
not send any postmessages to this thread - or better the thread didn`t
process these messages.
How is it possible to write this function in an ui-thread, so that the
ui thread is always able to get / process any postmessages?
Any kind of static methods is not the solution because I can`t change
the "threadID" of the PostThreadMessage to write this message to
threadA or threadB; because I`ve no access to any class members of the
ui thread within a static method.
best regards
Hans