如何在 C# 中启动进程,但仅在卸载所有程序集后才运行该进程?
我有一个程序可以从一个已加载的程序集中生成另一个进程。第二个进程会获取生成它的程序的安装程序(还困惑吗?)。第二个进程只是在特定的网址启动安装程序:
System.Diagnostics.Process.Start(www.mySite.com/myInstaller.exe);
问题是,如果在卸载父程序的程序集之前运行此安装程序,它将无法正确安装,因为需要覆盖程序的几个程序集作为安装。
我们需要从父程序中调用这个过程。
我的问题是如何生成这个进程,以便它仅在程序集卸载后运行?有没有更好的方法来完成这一切?
I have a program that spawns another process from within one of the loaded assemblies. This second process goes and grabs an installer for the program that spawned it (confused yet?). This second process just starts the installer at a specific web address:
System.Diagnostics.Process.Start(www.mySite.com/myInstaller.exe);
The problem is that if this installer is run before the parent program's assemblies have been unloaded it will not install correctly as a couple of the program's assemblies need to be overwritten as part of the install.
We need this process to be called from our parent program.
My question is how do I spawn this process so that it runs only after the assemblies have unloaded? Is there a better way to do all this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
子进程中检查父进程是否退出。
假设您知道应用程序的名称,这将是一个简单的检查:
Check in the child process whether the parent process has exited.
Assuming that you know the name of your application this would be a simple check:
基本上,您有一个“自我更新”的应用程序。您的主应用程序调用更新程序,该更新程序会找到安装程序并运行它。然后安装程序运行,并且可以轻松替换调用更新程序的程序集和更新程序。安装程序完成后,您可以重新启动应用程序。
您可以(.NET 默认情况下)使用操作系统 shell 来启动新进程。该进程与其父进程没有任何连接;例如,如果您创建了一个可执行文件,其唯一目的是打开记事本,则您的可执行文件将调用 Process.Start("notepad.exe"),记事本将打开,您的进程将关闭,使记事本保持打开状态。
为了确保您的应用程序已关闭,您可以在安装程序中使用 Process.GetProcesses(),这将获取您使用任务管理器看到的进程列表。扫描列表中您的应用程序的可执行文件名称(以及更新程序(如果适用));如果存在,请尝试休眠一秒钟,然后重试。还在吗?提醒用户手动关闭应用程序,然后当他们说已经关闭时,再次检查。
So basically, you have an application that "self-updates". Your main app calls an updater, which goes and finds the installer and runs that. The installer then runs, and could easily replace the assembly that called the updater, and the updater. When the installer's done, you restart the application.
You can (and .NET does by default) use the OS shell to start the new process. This process will have no connection to its parent; if, for instance, you created an executable whose sole purpose was to open Notepad, your executable would call Process.Start("notepad.exe"), Notepad would open, and your process would close, leaving Notepad open.
To make sure your application has closed, you can use Process.GetProcesses() in the installer, which will get the list of processes you'd see by using the Task Manager. Scan the list for your application's executable name (and if applicable, the updater); if it exists, try sleeping for a second, then try again. Still there? Alert the user to manually close the application, then when they say they have, check again.
如果您的安装程序是“愚蠢的”,则制作另一个应用程序纯粹是为了在主程序完全退出后启动安装程序。
尽管如此,我的许多软件都有自动更新,并且从未遇到过这个问题。
If your installer is "dumb" make another app purely to launch the installer once the Main program is fully exited.
Though having said that, I have an auto updated in many of my bits of software and have never had this problem.
Mutex 是一个类专为进程间同步而设计。它本质上是在所有正在运行的进程中创建命名锁。按名称检查正在运行的进程似乎有点脏(如果有人重命名您的应用程序怎么办)。
示例用法:
只要您的应用程序和安装程序都使用它,就应该没问题。 (如果您想允许应用程序的多个实例,请不要执行“无法继续 - MyMutex 正在您的应用程序中使用,仅在您的安装程序中使用”)。
Mutex is a class designed for inter-process synchronization. It essentially creates named lock across all running processes. Checking running processes by name seems a bit dirty (what if someone renames your application).
Sample usage:
As long as both your application and your installer are using that, you should be fine. (If you want to allow multiple instances of your application, don't do the "Can't coninue - MyMutex is in use in your application, just in your installer).