下面这个是线程的终止操作:
//DWORD id;
//GetExitCodeThread(hHidThread,&id);
//TerminateThread(hHidThread,id);
//hHidThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CHid::ThreadFunc, m_pHid, 0, NULL);
//CloseHandle(hHidThread);
//hHidThread=NULL;
然后创建线程是:
hHidThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CHid::ThreadFuncSourceData, m_pHid, 0, NULL);
还有就是:
static int flag = 1;
if(flag)
{
SuspendThread(hHidThread);
flag = 0;
}
else
{
ResumeThread(hHidThread);
flag = 1;
}
这种写法很好的利用了static的性质,从而有效的起到了开关作用。
unsigned char buf[512] = {0}; //数据缓冲区
CHid *phid = (CHid *)lpara; //hid设备指针
DWORD DataLen = phid->m_pDlg->m_uSensorNum * phid->m_pDlg->m_uDriverNum * 2;//定义DataLen长度
while(1)
{
phid->ReadHid(buf, 64); //读Hid函数
phid->m_pDlg->m_CriticalSection.Lock(); //加临界锁
for(DWORD i = 0, k = 0; i < DataLen; i += 2, k++ )
{
g_DataBuf[k]=(short)((buf[i]<<8) +buf[i+1]); //guitar大小端与PC相反
}
phid->m_pDlg->m_CriticalSection.Unlock(); //解锁
PostMessage(phid->m_pDlg->m_hWnd,WM_MY_UPDATEDATA,0,0); //发送更新显示消息
Sleep(10);
}
return 0;
上面用到了临界锁,暂时还不是很懂,后面再看吧。