Reputation:
I'm trying to execute a system command throught a program then wait till the process is terminated to carry on executing code's instructions. I've been using sleep()
but it didn't work out because it was relative i mean execution time differs from machine to another ...so is there any solution for this?
Consider code below(language==c++):
ShellExecute(0, "open", "cmd.exe","/C rasdial adsl user pwd", 0, SW_HIDE); //can also use system().
Sleep(sec);
if(CheckConnection()) {cout <<"U r connected"; }
Wait till system command is executed to check for connection (I think you get now).
Upvotes: 1
Views: 352
Reputation: 23624
Using CreateProcee
you have more options see this example http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx to wait program accomplishment with WaitForSingleObject
Upvotes: 0
Reputation: 6293
Use ShellExecuteEx instead of ShellExecute, and then call WaitForSingleObject with the hProcess you receive:
SHELLEXECUTEINFO info = { sizeof(SHELLEXECUTEINFO) };
// fill in values in SHELLEXECUTEINFO as necessary
if (ShellExecuteEx(&info))
{
WaitForSingleObject (info.hProcess, INFINITY);
// The new process has now completed
}
else
{
// Launch failed
}
Upvotes: 6