1

I noticed in my Task Manager I have several copies of this app - though not take any CPU resources.

I know I must be doing something wrong, so I ask the collective...

Here is the sterilized code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace AnalyticsAggregator
{
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        try
        {
            bool onlyInstance = false;
            Mutex mutex = new Mutex(true, "AnalyticsAggregator", out onlyInstance);
            if (!onlyInstance)
            {
                return;
            }

                            "Do stuff with the database"


            GC.KeepAlive(mutex);
        }
        catch (Exception e)
        {

                EventLog eventlog = new EventLog("Application");
                eventlog.Source = "AnalyticsAggregator";
                eventlog.WriteEntry(e.Message, EventLogEntryType.Error);
            }
        }
    }
}
}

I have other console apps that are not mutex/singleton that exhibit the same behavior, what am I doing wrong? I am assuming some type of disposal...

Thanks

Buddy Murphy
  • 57
  • 2
  • 8
  • 3
    Why do you have that `GC.KeepAlive(mutex)` in there? – feralin Mar 25 '13 at 16:20
  • I copied and pasted your code into Visual Studio, and replaced that `"Do Stuff"` line with a long `Thread.Sleep(100000)`. It worked as expected, where only one instance of the application would be running. Can you post code from which the problem can be reproduced, as well as more information about your environment? – Christopher Currens Mar 25 '13 at 16:29
  • The examples I've seen, and then used, had the GC.KeepAlive(mutex); is this wrong? – Buddy Murphy Mar 25 '13 at 16:59
  • In the MSDN documentation for `GC.KeepAlive(object)` (http://msdn.microsoft.com/en-us/library/system.gc.keepalive(v=vs.100).aspx) it looks like you should only use it if you are calling unmanaged code that might hang on to the specified object. Are you using any COM code or unmanaged DLL's in the "Do stuff with the database" code? – feralin Mar 25 '13 at 18:29
  • @feralin No, just TableAdapter calls. We currently use TableAdapters as our DAL. – Buddy Murphy Mar 26 '13 at 14:33
  • @BuddyMurphy I don't know if using a TableAdapter works as part of the criteria for using GC.KeepAlive(). It doesn't sound right to me, but, then again, the question isn't about the GC.KeepAlive() call :) – feralin Mar 26 '13 at 14:44

3 Answers3

2

A console application will just terminate when it's done, generally when it runs to the end of its Main entry point method, though you must be careful to get rid of any lingering resources you might have been managing, as they can keep the underlying process alive.

You can explicitly exit using Environment.Exit, and also Application.Exit although the latter is Forms-based from my experience (relating to clearing message pumps and closing windows etc.).

Bottom line is to be sure to do housekeeping.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

You could try adding a using statement around the use of the Mutex. Example:

using (Mutex mutex = new Mutex(true, "AnalyticsAggregator", out onlyInstance))
{
    if (!onlyInstance) return;
    ... database stuff ...
}

This will automatically dispose of the Mutex instance when the code inside the curly brackets exits by any means (normal execution, or an exception).

feralin
  • 3,268
  • 3
  • 21
  • 37
0
Application.Exit();

Is the standard approach, however unless you're looking to exit the app through user input (ex. Do you want to quit the app (y/n)?), closing the console should prove sufficient.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • @feralin I think you are correct, the database connections are not being released correctly. – Buddy Murphy Mar 25 '13 at 17:09
  • Wouldn't application.exit act as a catch all here then? In regards to the db, the connection would be left hanging till the db server timed it, but that wasn't the question I thought. Anyways if application.exit doesn't work for some reason, Environment.Exit(0) is the more extreme form, kind of like a kill command. – RandomUs1r Mar 25 '13 at 17:49