CLR Via CSharp读书笔记(1):CLR的执行模型

本文详细介绍了三种防止C#程序运行多个实例的方法:使用线程互斥变量、采用判断进程的方式和全局原子法。每种方法都附有代码示例,帮助开发者实现程序的单实例运行。

ch01-1-WinInstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i "C:\Program Files (x86)\YourCompany\WinServiceFolder\WinServiceName.exe"

echo ---------------------------------------------------
pause
echo Done.

ch01-2-WinUnInstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u "C:\Program Files (x86)\YourCompany\WinServiceFolder\WinServiceName.exe"

echo ---------------------------------------------------
pause
echo Done.

实现C#程序只允许运行一个实例的几种方法详解 

方法一:
使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.
把program.cs文件里的Main()函数改为如下代码:

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

namespace TempTest
{
    class Program
    {
        static void Main(string[] args)
        {
            bool runone;
            Mutex run = new Mutex(true, "single_test", out runone);

            if (runone) // 判断runone是关键
            {
                run.ReleaseMutex();
                Console.WriteLine("My Temp Test");
                Console.ReadLine();
            }
        }
    }
}

方法二:采用判断进程的方式,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同,如是没有运行该程序,如果有就就不运行.在C#中应用System.Diagnostics名字空间中的Process类来实现,

主要代码如下: 

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

namespace TempTest
{
    class Program
    {
        static void Main(string[] args)
        {
            if (RunningInstance() == null)
            {
                Console.WriteLine("My Temp Test");
                Console.ReadLine();
            }
        }

        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.Id != current.Id) // 忽略当前进程
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", @"/") == current.MainModule.FileName)
                    {
                        return process; // 返回其他进程实例
                    }
                }
            }
            return null;
        }
    }
}

方法三:全局原子法,创建程序前,先检查全局原子表中看是否存在特定原子A(创建时添加的),存在时停止创建,说明该程序已运行了一个实例;不存在则运行程序并向全局原子表中添加特定原子A;退出程序时要记得释放特定的原子A哦,不然要到关机才会释放。C#实现如下:

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

namespace TempTest
{
    class Program
    {
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern UInt32 GlobalAddAtom(String lpString); //添加原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern UInt32 GlobalFindAtom(String lpString); //查找原子

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //删除原子

        static void Main(string[] args)
        {
            UInt32 aAtom = GlobalFindAtom("temp_test");
            if (aAtom != 0) // 找到原子("temp_test")
            {
                return;
            }
            GlobalAddAtom("temp_test");
            Console.WriteLine("My Temp Test");
            Console.ReadLine();

            GlobalDeleteAtom(aAtom);
        }
    }
}

 

转载于:https://www.cnblogs.com/thlzhf/archive/2012/11/25/2787246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值