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);
        }
    }
}

 

posted on 2012-11-25 09:54  逝者如斯(乎)  阅读(585)  评论(0编辑  收藏  举报