Question

The answer probably involves a custom uninstall action, but it seems like such a major oversite and something that every application would want, so I thought I'd see if there is something easy I am missing.

I have a couple of projects create in Visual Studio 2008 using C#, Winform projects. One uses click-once install, and one use a setup project. They both allow you to leave the application running when you uninstall. On Windows 7 you do get a warning, but have the option to leave it running anyway... on XP not even a warning.

Call me crazy, but the thought of all of my program files being removed, and my app still left running, doesn't sound like a good design at all. Memories of GPFs come to mind, or the way .NET uses delay loading for DLLs, also could be a problem when the DLL needs to be loaded, and is gone.

So is there an easy way to not allow uninstall if your app is still running?

I'm using C# but I'm using the VB.NET Single Instance class to make sure my app only runs once, so maybe something that takes advantage of that as opposed to creating a mutex to check for?

Also my app only allows "per user" installs, not "all users", so I shouldn't have to worry about my app running in another session (if it is, it is OK to stay running because it should be 100% its own copy).

Edit: Since I'm not finding anything else, I've tried the mutex idea and it isn't working. Here is my code:

In the app I have this to create the named mutex and keep alive while app running:

        System.Threading.Mutex m = new System.Threading.Mutex(true, "SafeShare");

        SingleInstanceApplication.Run(TabForm, NewInstanceHandler);

        GC.KeepAlive(m);

In the uninstall action I have this to loop until they exit (since it doesn't appear to be easy to provide a cancel option):

            while (true)
            {
                bool createdNew;
                System.Threading.Mutex m = new System.Threading.Mutex(true, "SafeShare", out createdNew);
                m.ReleaseMutex();
                if (createdNew)
                    break;
                MessageBox.Show(null,"Close XXX and click OK to continue uninstall.","Uninstall");
            }

createdNew is never set in the uninstall action, almost like it is running under a different session or something. I haven't tried adding Global\ to the mutex name, but that would not work well for my app anyway because I do per-user installs, so one install should be able to keep running on another session even if one is uninstalled.

Was it helpful?

Solution

Short answer is there is no easy way to do this because it isn't something Microsoft wants you to do.

My answer did end up using a mutex, once I got the code correct it seems to work fine.

In the app I do:

System.Threading.Mutex m = new System.Threading.Mutex(true, "MyMutexNameHere");
SingleInstanceApplication.Run(TabForm, NewInstanceHandler);
GC.KeepAlive(m); 

The SingleInstanceApplication.Run line is what runs your app, I'm using the VB.NET framework.

In the custom uninstall action I do:

while (true)
{
   bool createdNew;
   System.Threading.Mutex m = new System.Threading.Mutex(true, "MyMutexNameHere", out createdNew);
   m.Close();
   if (createdNew)
      break;
   MessageBox.Show(null,"Close XXX and click OK to continue uninstall.","Uninstall");
}    

At first I was using m.ReleaseMutex() instead of m.Close() which I found out was throwing an exception because I didn't own the mutex. Close was what I should have been using, just to make sure my copy was closed before I checked again (otherwise createNew was never true, if it wasn't true the first time).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top