码迷,mamicode.com
首页 > Windows程序 > 详细

WINFORM 应用程序唯一执行验证

时间:2017-08-25 13:43:37      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:info   执行   lease   process   create   returns   mutex   assembly   param   

通过应用程序名称创建Mutex实现跨程序标识.
program中的调用方法:
SingleProcessFixer singleProcessFixer = new SingleProcessFixer();
if (!singleProcessFixer.CreateMutex()) //程序正在运行.
 
public class SingleProcessFixer 
{
    /// <summary>
    /// core mutex
    /// </summary>
    Mutex mutex = null;
 
    /// <summary>
    /// Create mutex in currect assembly and return if create successful.
    /// </summary>
    /// <returns>if create successful</returns>
    public bool CreateMutex()
    {
        return CreateMutex(Assembly.GetEntryAssembly().FullName);
    }
 
    /// <summary>
    /// Create mutex by name and return if create successful.
    /// </summary>
    /// <param name="name">name for mutex</param>
    /// <returns>if create successful.</returns>
    public bool CreateMutex(string name)
    {
        bool result = false;
        mutex = new Mutex(true, name, out result);
        return result;
    }
 
    /// <summary>
    /// Release mutex.
    /// </summary>
    public void ReleaseMutex()
    {
        if (mutex != null)
        {
            mutex.Close();
        }
    }
}

WINFORM 应用程序唯一执行验证

标签:info   执行   lease   process   create   returns   mutex   assembly   param   

原文地址:http://www.cnblogs.com/sunlyk/p/7427302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!