Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check for process before Install App

Posted on 2009-07-14
6
Medium Priority
?
445 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:cholden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24853145
By "install" do you mean: you are generating an MSI or exe using an installer project, or is this something else?
0
 

Author Comment

by:cholden
ID: 24853328
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
0
 
LVL 6

Accepted Solution

by:
ahmad2121 earned 2000 total points
ID: 24853433
So you need to set a custom install action. The easiest way of doing this:

if your project (main project, not setup project) has an Installer.cs (or Installer1.cs), skip the step below, if it doesn't you need to add one.

to add:

right click on project > Add > New Item > Installer Class

Now, right click on Installer.cs (or Installer1.cs), click "View Code"

type the following:

        public override void Install(IDictionary stateSaver)
        {
            //close process code here
            base.Install(stateSaver);
        }

replace the comment with your code.

you could do it in the Commit stage

        public override void Commit(IDictionary savedState)
        {
            //close process code here
            base.Commit(savedState);
        }

which does it before the install happens, and not when they execute the installer.

For more info, look here:

http://msdn.microsoft.com/en-us/library/d9k65z2d.aspx
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:cholden
ID: 24853530
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
0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24853672
kill does a force, try:

p.CloseMainWindow()

or

p.Close()
0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24853896
there is a more clever solution. In the OnClose or OnClosing method, you could do this:

notifiyIcon.Visible = false;
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question