代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Terry{
    
class Program
    {
        
private Mutex mutex = null;

        
public Program()
        {
            mutex 
= new Mutex(false"TerrySoft_MUTEX");
            
if (!mutex.WaitOne(0false))
            {
                mutex.Close();
                mutex 
= null;
            }
        }

        
public void ReleaseMutex()
        {
            
if (mutex != null)
            {
                mutex.ReleaseMutex();
                mutex 
= null;
            }
        }

        
~Program()
        {
            ReleaseMutex();
        }

                
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>
        [STAThread]
        
static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);

            Program cm 
= new Program();
            
if (cm.mutex == null)
            {
                MessageBox.Show(
"已经有一个程序在运行中。");
            }
            
else
            {
                Application.Run(
new frmStart());
            }
            cm.ReleaseMutex();
//手工释放Mutex

        }

    }
}

 

posted on 2010-03-31 11:36  Gu  阅读(476)  评论(1编辑  收藏  举报