Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / Windows Forms

Task Manager

Rate me:
Please Sign up or sign in to vote.
4.90/5 (105 votes)
1 Jan 2010CPOL4 min read 204.8K   21K   229  
Manage your daily tasks and To-Do list using some exciting features of Windows 7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Reflection;

namespace TaskManager.Common
{
    /// <summary>
    /// Class to insure the single instance of the application
    /// </summary>
    public class SingleInstanceChecker : IDisposable
    {

        #region Data Members

        private Mutex _mutex;
        private bool _isNew = false;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public SingleInstanceChecker()
            : this(string.Empty)
        {
        }

        /// <summary>
        /// Constructor to get process name
        /// </summary>
        /// <param name="name">name of process/application</param>
        public SingleInstanceChecker(string name)
        {
            string mutexName = Assembly.GetExecutingAssembly().GetName().Name + name;
            _mutex = new Mutex(true, mutexName, out _isNew);
        }

        #endregion

        #region Destructor

        /// <summary>
        /// Destructor to release mutex
        /// </summary>
        ~SingleInstanceChecker()
        {
            ReleaseMutex();
        }

        #endregion

        #region Properties

        /// <summary>
        /// get true it is first instance
        /// </summary>
        public bool IsSingle
        {
            get { return _isNew; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Relase owned mutex
        /// </summary>
        private void ReleaseMutex()
        {
            if (_isNew)
            {
                _mutex.ReleaseMutex();
                _isNew = false;
            }
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// dispose and release mutex
        /// </summary>
        public void Dispose()
        {
            ReleaseMutex();
            GC.SuppressFinalize(this);
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions