C++'s “system” without wait (Win32)

后端 未结 3 986
忘掉有多难
忘掉有多难 2021-01-14 21:34

I have got a program which checks if there\'s a version update on the server. Now I have to do something like

if(update_avail) {
    system(\"updater.exe\");         


        
3条回答
  •  隐瞒了意图╮
    2021-01-14 22:02

    You need a thread for that Look here: http://msdn.microsoft.com/en-us/library/y6h8hye8(v=vs.80).aspx You are currently writing your code in the "main thread" (which usually is also your frame code). So if you run something that takes time to complete it will halt the execution of your main thread, if you run it in a second thread your main thread will continue.

    Update: I've missed the part that you want to exit immediately. execl() is likely what you want.

    #include 
    
    int main(){
    
        execl("C:\\path\\to\\updater.exe", (const char *) 0);
        return 0;
    }
    

    The suggested CreateProcess() can be used as well but execl is conforming to POSIX and would keep your code more portable (if you care at all).

    #include 
    extern char **environ;
    int execl(const char *path, const char *arg, ...);
    

    Update: tested on Win-7 using gcc as compiler

提交回复
热议问题