Link to home
Start Free TrialLog in
Avatar of cholden
cholden

asked on

Check for process before Install App

Here is my scenario. A user installs the c# application from my website. After install the application starts (hidden) and puts an icon in the system tray. Now lets say the user goes back to the website and clicks install again. I want a way to check and kill the process if there is one before the install. I have the code to kill a process but not sure on where to put it before the install happens.

Thanks

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            int x = 0;
 
            foreach (Process p in System.Diagnostics.Process.GetProcessesByName("NameOfApp"))
            {
                if (x > 0)
                {
 
                    try
                    {
                        p.Kill();
                        p.WaitForExit(); // possibly with a timeout
                    }
                    catch (Win32Exception winException)
                    {
                        // process was terminating or can't be terminated - deal with it
                    }
                    catch (InvalidOperationException invalidException)
                    {
                        // process has already exited - might be able to let this one go
                    }
 
                }
                x++;
 
 
            }
            
 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

Open in new window

Avatar of ahmad2121
ahmad2121

By "install" do you mean: you are generating an MSI or exe using an installer project, or is this something else?
Avatar of cholden

ASKER

Thanks for the quick reply.

I right click to build the project. This creates the msi and exe in the release folder. When a user clicks the install button on the website it links to setup.exe and installs the application on the users computer.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of ahmad2121
ahmad2121

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cholden

ASKER

I tried putting the code int the public override void Install before and that didn't work so I put it in the Commit and it works great. Now the only thing is that the killed process icon is still in the system tray until you move the mouse over it. Is there a way I can get rid of that icon when process is killed?

Thanks
kill does a force, try:

p.CloseMainWindow()

or

p.Close()
there is a more clever solution. In the OnClose or OnClosing method, you could do this:

notifiyIcon.Visible = false;