📄 threadmgr.cpp
字号:
// ThreadMgr.cpp: implementation of the CThreadMgr class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ThreadMgr.h"
#include "Utils/SerialCommClass.h"
#include "Utils/Power.h"
#include "Utils/Report.h"
#define BUF_LEN 1024
HANDLE g_hStopEvent = NULL;
HANDLE g_hThread = NULL;
HANDLE g_hThread1 = NULL;
HANDLE g_hThread2 = NULL;
HANDLE g_hThread3 = NULL;
CSerialComm m_port;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CThreadMgr::CThreadMgr()
{
}
CThreadMgr::~CThreadMgr()
{
}
/////////////////////////////////////////////////////////////////////////////
// Thread function
UINT Thread(LPVOID pParam)
{
// Continue work loop until signaled to stop.
while (PowerMgr.WaitForSingleObject(g_hStopEvent, 0) == WAIT_TIMEOUT)
{
// Typically, sleep until time to do work. Sleep is required
// at some point in order to give other threads time to run.
// Sleep may be interrupted by an event (engine start or some
// other signal) by using either WaitForSingleObject() or
// WaitForMultipleObjects().
DWORD dwSleep = 1000;
PowerMgr.Sleep(dwSleep);
// Do work...
Report.Debug(_T(" This is Thread 1 !"));
// Make measurement.
/*m_port.SetCommPort(_T("COM1:"));
m_port.SetConfiguration(CBR_115200, 8, NOPARITY, ONESTOPBIT, FALSE);
m_port.OpenComm();
m_port.FlushInput();
COMMTIMEOUTS CommTimeouts;
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutConstant = 300;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
m_port.SetTimeouts(CommTimeouts);
// Signal sensor to return measurement data.
m_port.PutStr("O\r");
TCHAR strData[BUF_LEN];
int iNumChars = m_port.GetStr(strData, BUF_LEN - 1);
{
// strData now contains measurement, process as required.
}
m_port.CloseComm();
// <Buffer of output data not shown>*/
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//
//
UINT Thread1(LPVOID pParam)
{
// Continue work loop until signaled to stop.
while (PowerMgr.WaitForSingleObject(g_hStopEvent, 0) == WAIT_TIMEOUT)
{
// Typically, sleep until time to do work. Sleep is required
// at some point in order to give other threads time to run.
// Sleep may be interrupted by an event (engine start or some
// other signal) by using either WaitForSingleObject() or
// WaitForMultipleObjects().
DWORD dwSleep = 1000;
PowerMgr.Sleep(dwSleep);
// Do work...
CTime t = CTime::GetCurrentTime();
Report.Debug(_T("Time %02d:%02d:%02d This is Thread 2 !"),t.GetHour(),t.GetMinute(),t.GetSecond());
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//
//
UINT Thread2(LPVOID pParam)
{
// Continue work loop until signaled to stop.
while (PowerMgr.WaitForSingleObject(g_hStopEvent, 0) == WAIT_TIMEOUT)
{
// Typically, sleep until time to do work. Sleep is required
// at some point in order to give other threads time to run.
// Sleep may be interrupted by an event (engine start or some
// other signal) by using either WaitForSingleObject() or
// WaitForMultipleObjects().
DWORD dwSleep = 1000;
PowerMgr.Sleep(dwSleep);
// Do work...
Report.Debug(_T(" This is Thread 3 !"));
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
//
//
UINT Thread3(LPVOID pParam)
{
// Continue work loop until signaled to stop.
while (PowerMgr.WaitForSingleObject(g_hStopEvent, 0) == WAIT_TIMEOUT)
{
// Typically, sleep until time to do work. Sleep is required
// at some point in order to give other threads time to run.
// Sleep may be interrupted by an event (engine start or some
// other signal) by using either WaitForSingleObject() or
// WaitForMultipleObjects().
DWORD dwSleep = 1000;
PowerMgr.Sleep(dwSleep);
// Do work...
Report.Debug(_T(" This is Thread 4 !"));
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// App Init and Exit Callbacks
extern "C" _declspec(dllexport) void AppInit()
{
// Called when Xpert boots. Objective is to start thread.
g_hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Reset event used by thread to know when to stop.
ResetEvent(g_hStopEvent);
// Spawn thread.
CWinThread* pThread = AfxBeginThread(Thread, NULL);
CWinThread* pThread1 = AfxBeginThread(Thread1, NULL);
CWinThread* pThread2 = AfxBeginThread(Thread2, NULL);
CWinThread* pThread3 = AfxBeginThread(Thread3, NULL);
if (pThread)
g_hThread = pThread->m_hThread;
if (pThread1)
g_hThread1 = pThread1->m_hThread;
if (pThread2)
g_hThread2 = pThread2->m_hThread;
if (pThread3)
g_hThread3 = pThread3->m_hThread;
}
extern "C" _declspec(dllexport) void AppExit()
{
// Signal thread to quit.
SetEvent(g_hStopEvent);
// Called when Xpert shuts-down. Objective is to stop thread.
if (g_hThread)
{
// Wait for thread to complete.
PowerMgr.WaitForSingleObject(g_hThread, INFINITE);
g_hThread = NULL;
// Wait for thread to complete.
}
// Called when Xpert shuts-down. Objective is to stop thread.
if (g_hThread1)
{
// Wait for thread to complete.
PowerMgr.WaitForSingleObject(g_hThread1, INFINITE);
g_hThread1 = NULL;
// Wait for thread to complete.
}
if (g_hThread2)
{
// Wait for thread to complete.
PowerMgr.WaitForSingleObject(g_hThread2, INFINITE);
g_hThread2 = NULL;
// Wait for thread to complete.
}
if (g_hThread3)
{
// Wait for thread to complete.
PowerMgr.WaitForSingleObject(g_hThread3, INFINITE);
g_hThread3 = NULL;
// Wait for thread to complete.
}
CloseHandle(g_hStopEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -