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

6
  • 3
    Why do you have that GC.KeepAlive(mutex) in there? Commented Mar 25, 2013 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? Commented Mar 25, 2013 at 16:29
  • The examples I've seen, and then used, had the GC.KeepAlive(mutex); is this wrong? Commented Mar 25, 2013 at 16:59
  • In the MSDN documentation for GC.KeepAlive(object) (msdn.microsoft.com/en-us/library/…) 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? Commented Mar 25, 2013 at 18:29
  • @feralin No, just TableAdapter calls. We currently use TableAdapters as our DAL. Commented Mar 26, 2013 at 14:33

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

lingering resources - like a DB connection? I'm looking into that right now.
@BuddyMurphy Precisely - something like that; make sure you call Dispose on IDisposables.
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).

Comments

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.

2 Comments

@feralin I think you are correct, the database connections are not being released correctly.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.