Programm1 starten, 10 Sekunden warten, Programm 2 starten

Hi, ich hab noch mal etwas daran gewerkelt, Smells entfernt und es idiotensicher gemacht. Ich hoffe, es hilft dir...:

C++:
#include <windows.h>
#include <iostream>
#include <string>

#include <chrono>
#include <thread>

DWORD WINAPI myThread(LPVOID lpParameter)
{
    using namespace std;
    char* argv = (char*)lpParameter;
    chrono::milliseconds ms1 = chrono::duration_cast<chrono::milliseconds>(
        chrono::system_clock::now().time_since_epoch());

    SHELLEXECUTEINFO ShExecInfo = { 0 };
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = argv;
    ShExecInfo.lpParameters = "";
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL;
    ShellExecuteEx(&ShExecInfo);
    WaitForSingleObject(ShExecInfo.hProcess, INFINITE);

    chrono::milliseconds ms2 = chrono::duration_cast<chrono::milliseconds>(
        chrono::system_clock::now().time_since_epoch());
    cout << argv << " : ";
    cout << GetProcessId(ShExecInfo.hProcess) << " : ";
    cout << (ms2 - ms1).count() << " Milliseconds" << endl;
    return 0;
}

int main(int argc, char* argv[])
{
    if (argc <= 1 || argc % 2 != 1) {
        return 0;
    }
    using namespace std;
    const int n1 = (argc - 1) / 2;
    HANDLE handles[n1];
    for (int i = 1; i < argc; i += 2) {
        int sleepTime = atoi(argv[i + 1]);
        DWORD myThreadID;
        handles[(i - 1) / 2] = CreateThread(0, 0, myThread, argv[i], 0, &myThreadID);
        this_thread::sleep_for(chrono::seconds(sleepTime));
    }
    WaitForMultipleObjects(n1, handles, TRUE, INFINITE);
    for (int i = 0; i < n1; i++) {
        CloseHandle(handles[i]);
    }
    return 0;
}
 
  • Gefällt mir
Reaktionen: o0Julia0o
Zurück
Oben