Home > Back-end >  About the thread problem of multiple calls
About the thread problem of multiple calls

Time:10-09

I now work on a project, it is need to implement such a feature, , then in each motor movement called a thread, to display the running situation of the motor;

Int CXYtable: : OnXRun ()
{

CSpiderDoc XSpider;
G_GlobalPara. X_dir=m_x_dir. GetCurSel ();
G_GlobalPara. X_fre=GetPulse (IDC_X_FRE);
G_GlobalPara. X_pulse=GetPulse (IDC_X_PUL);
G_GlobalPara. StepDetectType=1;
Cstrings X_tiaoshi;
If (StepMotorDetect (1, g_GlobalPara. StepDetectType, g_GlobalPara x_dir, g_GlobalPara. X_fre, g_GlobalPara. X_pulse, 0,0,0,0,0)==TRUE)
{
X_tiaoshi. The Format (" X motor into debug state: ");
G_GlobalPara. M_pSideBar - & gt; AddMessage (X_tiaoshi);
If (g_GlobalPara. X_dir==0)
{
X_tiaoshi. The Format (direction: forward, "");
G_GlobalPara. M_pSideBar - & gt; AddMessage (X_tiaoshi);
}
The else
{
X_tiaoshi. The Format (direction: reversal, "");
G_GlobalPara. M_pSideBar - & gt; AddMessage (X_tiaoshi);
}
X_tiaoshi. The Format (" motion frequency is: % d (HZ) ", int (g_GlobalPara. X_fre));
G_GlobalPara. M_pSideBar - & gt; AddMessage (X_tiaoshi);
Pulse X_tiaoshi. The Format (" movement is: % d (a) ", g_GlobalPara. X_pulse);
G_GlobalPara. M_pSideBar - & gt; AddMessage (X_tiaoshi);
G_GlobalPara. M_pSideBar - & gt; AddMessage (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
G_GlobalPara. XY_DectXianCheng=TRUE;
GetDlgItem (ID_X_RUN) - & gt; The EnableWindow (FALSE);
GetDlgItem (ID_Y_RUN) - & gt; The EnableWindow (FALSE);
GetDlgItem (ID_Z_RUN) - & gt; The EnableWindow (FALSE);
GetDlgItem (ID_R_RUN) - & gt; The EnableWindow (FALSE);
G_GlobalPara. X_yundong=TRUE;
HANDLE WORKING_Hnd;
WORKING_Hnd=CreateThread (NULL, 0, StepDetect, NULL, 0, NULL);//into the threads here [note]
The CloseHandle (WORKING_Hnd);
}
return TRUE;
}
Then thread concrete procedure is:
DWORD WINAPI CXYtable: : StepDetect (LPVOID params)
{

//pMF=(CMainFrame *) AfxGetApp CMainFrame * () - & gt; M_pMainWnd;//by first gets the current frame pointer
//the CView * active=pMF - & gt; GetActiveView ();//to get the current view class pointer

CXYtable XYtable;
UCHAR PUCHAR inData=https://bbs.csdn.net/topics/new [512].
CCyUSBDevice * USBDevice=new CCyUSBDevice ();
Bool success=false;

OVERLAPPED inOvLap;
InOvLap. HEvent=CreateEvent (NULL, false, false, "CYUSB_IN");
For (; G_GlobalPara. XY_DectXianCheng;)
{
ZeroMemory (inData, 512);
LONG inlen;
Inlen=512;

UCHAR * inContext=USBDevice - & gt; BulkInEndPt - & gt; BeginDataXfer (inData inlen, & amp; InOvLap);
USBDevice - & gt; BulkInEndPt - & gt; WaitForXfer (& amp; InOvLap, 2000);
USBDevice - & gt; BulkInEndPt - & gt; SetXferSize (512);
Success=USBDevice - & gt; BulkInEndPt - & gt; FinishDataXfer (inData inlen, & amp; InOvLap inContext);
If (success& & G_GlobalPara. XY_DectXianCheng)
{
Cstrings huichuang;
If ((inData [1]==0 x59) & amp; & (inData [3]==0 x59) & amp; & (inData x59 [5]==0))
{
Huichuang. The Format (" X axis motor debugging is completed, time is: ");
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
Long ltime;
Time (& amp; Ltime);
Huichuang. The Format (" % s ", ctime (& amp; Ltime));
Huichuang. TrimRight ();
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
G_GlobalPara. M_pSideBar - & gt; AddMessage (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
G_GlobalPara. XY_DectXianCheng=FALSE;
Sleep (50000);
//if (active!=NULL)//get the current view class pointer to send the message
//active - & gt; PostMessage (WM_USERMESSAGE, 0, 0);//using PostMessage sends the message
//CXYtable: : SendMessage ();
break;
}
Else if ((inData [1]==0 x62) & amp; & (inData [3]==0 x62) & amp; & (inData x62 [5]==0))
{
Huichuang. The Format (" Z axis motor debugging is completed, time is: ");
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
Long ltime;
Time (& amp; Ltime);
Huichuang. The Format (" % s ", ctime (& amp; Ltime));
Huichuang. TrimRight ();
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
G_GlobalPara. M_pSideBar - & gt; AddMessage (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
G_GlobalPara. XY_DectXianCheng=FALSE;
Sleep (50000);

//C_Button. PostMessage (WM_USERMESSAGE, 0, 0).
break;

}
Else if ((inData [1]==0 x63) & amp; & (inData [3]==0 x63) & amp; & (inData x63 [5]==0))
{
Huichuang. The Format (" R axis motor debugging is completed, time is: ");
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
Long ltime;
Time (& amp; Ltime);
Huichuang. The Format (" % s ", ctime (& amp; Ltime));
Huichuang. TrimRight ();
G_GlobalPara. M_pSideBar - & gt; AddMessage (huichuang);
G_GlobalPara. M_pSideBar - & gt; AddMessage (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
G_GlobalPara. XY_DectXianCheng=FALSE;
Sleep (50000);
//C_Button. PostMessage (WM_USERMESSAGE, 0, 0).
break;

}
}
}
ZeroMemory (inData, 512);
The delete [] inData;
The CloseHandle (inOvLap. HEvent);
Return 0 xdead;
}
So when the program after receive the underlying hardware, can be software update,
But there is one problem: after I update the picture for the first time, feel after motor this button again, it will not be able to enter the thread,
I want to know my thread has ended, why can't again into the,,,
Halves please, thank!

CodePudding user response:

Thread will be destroyed after the end, if you want to continue to use, don't let the end of the thread, thread to sleep after you use it, the next in the awakening,
  • Related