I am having some issues regarding the sleep function. I have my application which executes an external command with some options:
str := 'C:\BERN52\MENU\menu.exe C:\BERN52\GPS\PAN\DAILY.INP C:\GPSUSER52\WORK\MENUAUX_DAILY.INP';
WinExec(Pansichar(str), SW_Shownormal);
After that when this process is finished I should kill it and continue with another things. I did the following:
Sleep(60000*StrToInt(Form1.Edit11.Text));
winexec('taskkill /F /IM menu.exe', SW_HIDE);
...
This sleeping time can be 4 minutes but also can be 2 days. Of cause the main window going to the 'not responding' mode during this time. Could anyone suggest to me how to do this in a proper way?
First off,
WinExec()
has been deprecated since 32bit Windows was first introduced. UseShellExecuteEx()
orCreateProcess()
instead. This also provides you with a process handle that you can use to detect when the spawned process terminates, and you can also use it to kill the process if your timeout elapses.If you absolutely must block your calling code while waiting, use
MsgWaitForMultipleObjects()
in a loop so you can still service the message queue:Otherwise, use a
TTimer
so the main message loop is not blocked:Otherwise, use a worker thread instead of a timer: