user2355950
user2355950

Reputation:

How to make the program waits till the command is executed

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

Answers (2)

Dewfy
Dewfy

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

jlahd
jlahd

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

Related Questions