简体   繁体   English

C ++的“系统”,无需等待(Win32)

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

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");
    exit(0);
}

but without waiting for "updater.exe" to complete. 但无需等待“ updater.exe”完成。 Otherwise I can't replace my main program because it is running. 否则我无法替换主程序,因为它正在运行。 So how to execute "updater.exe" and immediately exit? 那么如何执行“ updater.exe”并立即退出? I know the *nix way with fork and so on, how to do this in Windows? 我知道使用fork等的* nix方式,如何在Windows中做到这一点?

Use CreateProcess() , it runs asynchronously. 使用CreateProcess() ,它异步运行。 Then you would only have to ensure that updater.exe can write to the original EXE, which you can do by waiting or retrying until the original process has ended. 然后,您只需确保updater.exe可以写入原始EXE,可以通过等待或重试直到原始进程结束来执行此操作。 (With a grace interval of course.) (当然有宽限期。)

There is no fork() in Win32. Win32中没有fork() The API call you are looking for is called ::CreateProcess() . 您要查找的API调用称为::CreateProcess() This is the underlying function that system() is using. 这是system()使用的基础功能。 ::CreateProcess() is inherently asynchronous: unless you are specifically waiting on the returned process handle, the call is non-blocking. ::CreateProcess()本质上是异步的:除非专门等待返回的进程句柄,否则该调用是非阻塞的。

There is also a higher-level function ::ShellExecute() , that you could use if you are not redirecting process standard I/O or doing the waiting on the process. 还有一个更高级别的函数::ShellExecute() ,如果您不重定向进程标准I / O或等待进程,则可以使用该函数。 This has an advantage of searching the system PATH for the executable file, as well as the ability to launch batch files and even starting a program associated with a document file. 这具有在系统PATH中搜索可执行文件的优点,并且具有启动批处理文件甚至启动与文档文件关联的程序的能力。

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). 您需要一个用于该线程的线程。http: //msdn.microsoft.com/zh-cn/library/y6h8hye8(v=vs.80).aspx您当前正在“主线程”(通常是“主线程”)中编写代码。也是您的框架代码)。 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. execl()可能是您想要的。

#include <unistd.h>

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). 建议的CreateProcess()也可以使用,但是execl符合POSIX,并且可以使您的代码更具可移植性(如果您很在意的话)。

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

Update: tested on Win-7 using gcc as compiler 更新:使用gcc作为编译器在Win-7上进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM