Home > Back-end >  Help: why no Sleep function effect
Help: why no Sleep function effect

Time:10-13

Hello, everyone:
The younger brother when debugging a program to find a problem, is the Sleep function in your code seems doesn't work, please help to analyze,
Code request delay after 10 seconds, receiving data is not correct prompt an error window, actually generate the executable, appeared immediately after the execute button pop-up error window, as if the delay instruction has no effect, and this phenomenon is not on my computer, but in other people's computer screen, it was strange,



//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : Button4Click (TObject * Sender)
{
Char * lpath;
Lpath=(char *) LocalAlloc (LMEM_ZEROINIT, 200);
Strcpy (lpath, filepath);


Int state;
Strcpy (lpath, filepath);

Strcat (lpath, "\ \ pictures \ \ 222. TXT");
State=BPLA_SendFile (lpath);

Sleep (10000);
If (the state!=BPLA_OK)
{
Char Msg [20]={0};

Sprintf (Msg, "last errror=% d", the state);
ShowMessage (GetLastError ());
ShowMessage (Msg);


MessageBox (NULL, "the document printing failed!" , "tip:" MB_ICONINFORMATION);
return;
}

For (int num=1; Num{
State=BPLA_SendFile (lpath);
If (the state!=BPLA_OK)
{
MessageBox (NULL, "the document printing failed!" , "tip:" MB_ICONINFORMATION);
return;
}
}
Bpress=false;
LocalFree (lpath);
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

CodePudding user response:

Sleep to take up the main thread, can cause UI freeze, such delay is not science, use the following code instead of:

 DWORD dwTick=: : GetTickCount (); 
While (: : GetTickCount () - dwTick & lt; 10000)
{
Application - & gt; ProcessMessages ();
}

CodePudding user response:

It can also delay, and learn new ways... I use this, but it will freeze window,
Application - & gt; ProcessMessages ();
Sleep (5000);

CodePudding user response:

Sleep will suspend the current thread, so it is not recommended to use in the UI thread.

CodePudding user response:

Studied the

CodePudding user response:

Studied the

CodePudding user response:

Studied the
  • Related