本帖最后由 我是黑名单 于 2012-3-21 11:20 编辑
我写了个bulk方式的上位机,AfxBegin(ReadThread,this)之后,我单步到ReadThread线程函数后,发现卡死,debug的时候,一点击开启线程,程序就卡死,提示内存不能为written,有谁知道怎么回事啊void CSECGDlg::OnButton7()
{
if (XferThread) {
bLooping = false;
XferThread=NULL;
m_StartBtn.SetWindowText("启动采集");
} else {
int n = USBDevice->DeviceCount();
if (n!= 0)
{
USBDevice->Open(0);
OutEndpt = USBDevice->EndPoints[2];
InEndpt = USBDevice->EndPoints[6];
// Launch the looping thread (Calls XferLoop() function, above.)
if (USBDevice->IsOpen()) {
bLooping = true;
XferThread = AfxBeginThread(ReadThread, this);
}
m_StartBtn.SetWindowText("关闭采集");
}
}
}
UINT ReadThread( LPVOID params )
{
OVERLAPPED inOvLap;
CSECGDlg *dlg = (CSECGDlg *) params;
PUCHAR inData = new UCHAR[512];
ZeroMemory(inData,512);
inOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_IN");
bool success;
dlg->InEndpt->TimeOut = 0;
for (;dlg->bLooping;) {
LONG inlen,len;
inlen = len = 512;
UCHAR *inContext = dlg->InEndpt->BeginDataXfer(inData,inlen,&inOvLap);
dlg->InEndpt->WaitForXfer(&inOvLap,2000);
success = dlg->InEndpt->FinishDataXfer(inData,inlen, &inOvLap,inContext);
if (!success) dlg->bLooping = false;
}
CloseHandle(inOvLap.hEvent);
delete [] inData;
dlg->XferThread = NULL;
dlg->USBDevice->Close();
return true;
} |