How can I terminate a process before installing a program?

心已入冬 提交于 2019-12-23 23:03:15

问题


I'm working on a system tray application that will likely see frequent future updates. To that end, it would be desirable for our users to have the installer find and kill the application process before trying to install it.

This is the class that I have found for the custom actions of both starting the process after the install, and terminating it prior install.

public InstallerActions( ) : base( ) {
    System.Diagnostics.Debugger.Launch( );
    this.BeforeInstall += ( S, E ) => {
        System.Diagnostics.Debugger.Break( );
        /*IPC Process Termination Method Goes Here*/
    };
    this.Committed += ( S, E ) => {
        System.Diagnostics.Debugger.Break( );
        try {
            Directory.SetCurrentDirectory( Path.GetDirectoryName( Assembly.GetExecutingAssembly( ).Location ) );
            Process.Start( Path.GetDirectoryName( Assembly.GetExecutingAssembly( ).Location ) + @"\Program.exe" );
        } catch { return; } //Return on failure.
    };
}

It does not work.

I know that there are the Install, Commit, Rollback and Uninstall custom actions, and before when I've tried them I've had no success with them either. This class is contained within the main assembly of the application which has been added to the Custom Actions section of my Setup and Deployment project... but that's all that I've done. Is there something further that I need to do to make these actions get called? I've even attached the VS debugger to the installer, but I see that the class isn't even being called (or, at least, the break points aren't becoming active in the class code which to me says it's not being loaded).

How can I make this work?

EDIT 1 :

For Clarity, it was requested the type of the Setup Project. This is a VS 2013 Setup and Deployment project type:

See the highlighted project type.

Sorry for not being clear on that.

EDIT 2 :

It has been to me suggested that IPC > Process.Kill( ), so I will explore that method; however, the main problem remains : If Process.Kill( ) isn't being called, whatever other method that needs to be called would not be called either.

EDIT 3 :

According to this link I found, it seems that the custom actions WOULD be getting called - but at a stage far too late to do me any good. I did neglect to mention that I've been running this test with another instance of the program in question running in the background, and have been getting the notification that the install can't continue because of that running program. This, therefore, helps me to understand the reason the actions are failing is because they are never even gotten to; the installer can't terminate the program because the program stops the installer from progressing to the command to terminate the program because Catch 22.

So the question is still valid, but now I have to ask - is it even possible, like say, when the program installer is first being run, to terminate the process, or is it just something the user will have to do manually?

EDIT 4

Given what I now know, I am forced to switch from the VS Setup Project to WIX... I don't even know what WIX is beyond some other setup... tool... thing. Stay tuned for future developments...


回答1:


Killing a process is not a good idea. The process might be in progress of saving some data to a file which could get interrupted. Also, please be aware that there might be other processes with the same name that you're killing - it needn't be yours. This may cause serious data loss.

Try inter-process communication instead, e.g. a named pipe, a certain file in a known directory etc.

In .NET, you might want to try the IpcChannel class.



来源:https://stackoverflow.com/questions/31463550/how-can-i-terminate-a-process-before-installing-a-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!