Click here to Skip to main content
15,895,794 members

Help on multi thread mfc, serial comms, GUI unresponsive

Zsefv2 asked:

Open original thread
Dear fellow coders,

I am facing this problem with my program. The program is interfacing with another machine which is outputting serial data at 1Hz. My program is MFC based (a cpropertypage) and to handle the data, i have defined a thread (pRecvDataThread) to handle the reading of data from the serial port's buffer, and display it on my list control in the main thread.

I have separated GUI stuff like getting list control handles etc out of the thread as suggested in some tutorial. However my GUI still is hardly responsive. Heres the section of the code, does anyone know whats wrong?

C++
// on serial data arrival
LRESULT MyApp::OnDataArrived(WPARAM wParam,LPARAM lParam) 
{
	Sleep(40);
	const CSerialMFC::EEvent eEvent = CSerialMFC::EEvent(LOWORD(wParam));
	const CSerialMFC::EEvent eError = CSerialMFC::EEvent(HIWORD(wParam));

	switch(eEvent)
	{
	case CSerialMFC::EEventRecv:

		pRecvDataThread = AfxBeginThread(MyRecvThread, LPVOID(this), THREAD_PRIORITY_NORMAL);
		break;
	}

	return 0;
}

// my thread proc
UINT MyRecvThread(LPVOID pParam)
{

	MyApp *pTab = (MyApp*)pParam;
	WaitForSingleObject(pTab ->MutexRecv, INFINITE);
	DWORD dwBytesRead = 0;
	byte abBuffer[512];

	int errorInt = pTab->serialMFC.Read(abBuffer, sizeof(abBuffer), &dwBytesRead, 0, INFINITE);

	if(errorInt != ERROR_SUCCESS)
	{
		AfxMessageBox(_T("ERROR in reading serial port"));
		return 0;
	}

	char szTimestamp[128];
	SYSTEMTIME st;
	//GetSystemTime(&st);
	GetLocalTime(&st);
	sprintf_s(szTimestamp, 128, "%02d:%02d:%02d:%03d", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
	

	pTab->ReadData(abBuffer, szTimestamp, numBytesRead);

	ReleaseMutex(MyApp->MutexRecv);
	return 0;
}
void MyApp::ReadData(byte bytesRead [], char* timestamp, int bytesread)
{
    // display the bytes in list control
}
Tags: C++, MFC, GUI, Multi-threading, Serial port

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900