P
So habe immer noch ein paar Probleme mit meinen Threads.
Ich habe 3 Threads, eine Lese-,Schreibe- und einen Auswerte-Thread.
Der Schreibthread wartet, bis der Lesethread aufgehört hat und kann dann schreiben, falls neue Daten zum Schreiben anliegen. Umgekehrt funktioniert das mit dem Lesethread. Ich möchte aber, dass zusätzlich im Hintergrund, sobald ich was neues eingelesen habe, die Daten ausgewertet(dekodiert) werden.
Bisher habe ich das so gelöst, jedoch läuft der Auswertethread nicht im Hintergrund, sondern die Threads laufen nacheinander durch.
HANDLE hWrite, hRead, hDecode;
UINT WriterThread(LPVOID pParam);
UINT ReaderThread(LPVOID pParam);
UINT DecoderThread(LPVOID pParam);
BOOL MyClass::OnInitDialog()
{
.....
hGlobal = CreateEvent(NULL,TRUE,FALSE,NULL);
if (hGlobal == NULL)
MessageBox("Failed creating hGlobal event");
osStatus.hEvent = CreateEvent(NULL,TRUE,TRUE,NULL);
if (osStatus.hEvent == NULL)
AfxMessageBox("Failed creating osStatus.hEvent!");
....
}
void MyClass::OnBnClickedConnect()
{
m_fConnected = TRUE;
....
AfxBeginThread(ReaderThread,this);
AfxBeginThread(WriterThread,this);
AfxBeginThread(DecoderThread,this);
...
}
UINT ReaderThread(LPVOID pParam)
{
DWORD dwRes;
BYTE byReadByte;
CMyCLass* pWnd = (CMyCLass*)pParam;
if (!pWnd)
AfxMessageBox("Failed creating pointer to application!");
hRead = CreateEvent(NULL,FALSE,TRUE,NULL);
if (hRead == NULL)
AfxMessageBox("Failed creating hRead event!");
HANDLE hArray[2];
hArray[0] = pWnd->hGlobal;
hArray[1] = hWrite;
while (pWnd->m_fConnected)
{
dwRes = WaitForMultipleObjects(2,hArray,TRUE,INFINITE);
{
BOOL result = pWnd->com.ReadData(byReadByte,1);
if (result)
{
pWnd->byReadByte = byReadByte;
pWnd->byDecodeArray[pWnd->iArrayIndex++] = byReadByte;
if (pWnd->iArrayIndex == 255) pWnd->iArrayIndex = 0;
AfxBeginThread(DecoderThread,pWnd); // das ist eine doofe Lösung!!!
SetEvent(hRead); // Warum bei einem auto-event das gesetzt werden muss, ist mir auch nicht ganz klar?!?
}
}
}
return TRUE;
}
UINT WriterThread(LPVOID pParam)
{
BYTE DATA = 00;
DWORD dwWaitResult;
CMyCLass* pWnd = (CMyCLass*)pParam;
if (!pWnd)
{
AfxMessageBox("Failed creating pointer to application!");
return FALSE;
}
hWrite = CreateEvent(NULL,FALSE,FALSE,NULL);
if (hWrite == NULL)
AfxMessageBox("Failed creating hWrite event!");
while (pWnd->m_fConnected && DATA < 0xFF)
{
if (!ResetEvent(pWnd->hGlobal))
AfxMessageBox("Failed resetting hGlobal!");
dwWaitResult = WaitForSingleObject(hRead,INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
if(pWnd->com.WriteData(DATA++,1))
{
if (!SetEvent(pWnd->hGlobal))
AfxMessageBox("Failed setting hGlobal!");
}
break;
default:
AfxMessageBox("Wait error!");
return FALSE;
}
}
pWnd->SetWindowText("Beendet!");
return TRUE;
}
UINT DecoderThread(LPVOID pParam)
{
CMyCLass* pWnd = (CMyCLass*)pParam;
// HIER DATEN VERARBEITEN!!! ABER IM HINTERGRUND
return TRUE;
}
Falls noch Fragen offen sind, bitte stellen. Bin für jede Hilfe dankbar!
Wäre wirklich schon, wenn sich jemand die Zeit nehmen würde.