I'm trying to figure out why this won't work. I've coded up a little controller for my Matrix Orbital LCD display. Part of the deal is a CPU usage monitor. I was trying to spawn a new thread for the monitor's process so the program would still be responsive. Here's the code in question:
This is where I declare and start the monitor:
This is the StartCpuMonitor function in my CLCDControls class:
This is the MonitorThreadFunc() function, also in the CLCDControls class:
Neither of the message boxes ever pop up. The thread monitoring function does begin execution, but it doesn't appear to be a new thread. Or it's somehow completely stealing control from the calling function(s). Anybody see what I'm missing? This is using Visual Studio 2003, MFC, C++. Thanks in advance.
This is where I declare and start the monitor:
Code:
void CLCDControllerDlg::OnBnClickedButton1()
{
CLCDControls theControls;
theControls.StartCpuMonitor();
MessageBox("After StartCpuMonitor in OnBnClickedButton1()", "Debug Msg", IDOK);
}
This is the StartCpuMonitor function in my CLCDControls class:
Code:
void CLCDControls::StartCpuMonitor()
{
LPDWORD threadID = NULL;
SHCreateThread((LPTHREAD_START_ROUTINE)MonitorThreadFunc(), NULL, NULL, NULL);
MessageBox(NULL, "After CreateThread in StartCpuMonitor()", "Debug Msg", IDOK);
}
This is the MonitorThreadFunc() function, also in the CLCDControls class:
Code:
unsigned long CLCDControls::MonitorThreadFunc(void)
{
unsigned long byteCount = 0;
CString useMsg;
int usage = 0, lastUsage = 0;
double diff = 0;
CCpuUsage theMonitor;
byteCount += ClearDisplay();
byteCount += InitGraph();
byteCount += WriteLine((BYTE*)"CPU Use: ");
theMonitor.InitMonitor();
while (ContinueCpuMonitor)
{
usage = theMonitor.GetUsage();
diff = usage - lastUsage;
//Write the percentage out to the screen
useMsg.Format("%3d", usage);
useMsg += '%';
MoveCursor(10, 1);
byteCount += Write((BYTE*)useMsg.GetBuffer(), useMsg.GetLength());
//Smoothely move from lastUsage to usage over the next
// half second.
diff = diff / 5;
for (double i = 1; i <= 5; i++)
{
int percent = lastUsage + (diff * i);
byteCount += DrawGraph(1, 2, 0, percent);
Sleep(100);
}
lastUsage = usage;
}
return(byteCount);
}
Neither of the message boxes ever pop up. The thread monitoring function does begin execution, but it doesn't appear to be a new thread. Or it's somehow completely stealing control from the calling function(s). Anybody see what I'm missing? This is using Visual Studio 2003, MFC, C++. Thanks in advance.