C# SingleInstance类代码:
- usingSystem;
- usingSystem.IO;
- usingSystem.Diagnostics;
- usingSystem.Threading;
- usingSystem.Reflection;
- usingSystem.Runtime.InteropServices;
- namespaceZhengzuo.CSharpCode
- {
- ///<summary>
- ///只启动一个应用程序实例控制类
- ///</summary>
- publicstaticclassSingleInstance
- {
- privateconstintWS_SHOWNORMAL=1;
- [DllImport("User32.dll")]
- privatestaticexternboolShowWindowAsync(IntPtrhWnd,intcmdShow);
- [DllImport("User32.dll")]
- privatestaticexternboolSetForegroundWindow(IntPtrhWnd);
- //标志文件名称
- privatestaticstringrunFlagFullname=null;
- //声明同步基元
- privatestaticMutexmutex=null;
- ///<summary>
- ///staticConstructor
- ///</summary>
- staticSingleInstance()
- {
- }
- #regionapi实现
- ///<summary>
- ///获取应用程序进程实例,如果没有匹配进程,返回Null
- ///</summary>
- ///<returns>返回当前Process实例</returns>
- publicstaticProcessGetRunningInstance()
- {
- ProcessProcesscurrentProcess=Process.GetCurrentProcess();//获取当前进程
- //获取当前运行程序完全限定名
- stringcurrentFileName=currentProcess.MainModule.FileName;
- //获取进程名为ProcessName的Process数组。
- Process[]processes=Process.GetProcessesByName(currentProcess.ProcessName);
- //遍历有相同进程名称正在运行的进程
- foreach(Processprocessinprocesses)
- {
- if(process.MainModule.FileName==currentFileName)
- {
- if(process.Id!=currentProcess.Id)//根据进程ID排除当前进程
- returnprocess;//返回已运行的进程实例
- }
- }
- returnnull;
- }
- ///<summary>
- ///获取应用程序句柄,设置应用程序前台运行,并返回bool值
- ///</summary>
- publicstaticboolHandleRunningInstance(Processinstance)
- {
- //确保窗口没有被最小化或最大化
- ShowWindowAsync(instance.MainWindowHandle,WS_SHOWNORMAL);
- //设置真实例程为foregroundwindow
- returnSetForegroundWindow(instance.MainWindowHandle);
- }
- ///<summary>
- ///获取窗口句柄,设置应用程序前台运行,并返回bool值,重载方法
- ///</summary>
- ///<returns></returns>
- publicstaticboolHandleRunningInstance()
- {
- Processp=GetRunningInstance();
- if(p!=null)
- {
- HandleRunningInstance(p);
- returntrue;
- }
- returnfalse;
- }
- #endregion
- #regionMutex实现
- ///<summary>
- ///创建应用程序进程Mutex
- ///</summary>
- ///<returns>返回创建结果,true表示创建成功,false创建失败。</returns>
- publicstaticboolCreateMutex()
- {
- returnCreateMutex(Assembly.GetEntryAssembly().FullName);
- }
- ///<summary>
- ///创建应用程序进程Mutex
- ///</summary>
- ///<paramnameparamname="name">Mutex名称</param>
- ///<returns>返回创建结果,true表示创建成功,false创建失败。</returns>
- publicstaticboolCreateMutex(stringname)
- {
- boolresult=false;
- mutex=newMutex(true,name,outresult);
- returnresult;
- }
- ///<summary>
- ///释放Mutex
- ///</summary>
- publicstaticvoidReleaseMutex()
- {
- if(mutex!=null)
- {
- mutex.Close();
- }
- }
- #endregion
- #region设置标志实现
- ///<summary>
- ///初始化程序运行标志,如果设置成功,返回true,已经设置返回false,设置失败将抛出异常
- ///</summary>
- ///<returns>返回设置结果</returns>
- publicstaticboolInitRunFlag()
- {
- if(File.Exists(RunFlag))
- {
- returnfalse;
- }
- using(FileStreamfs=newFileStream(RunFlag,FileMode.Create))
- {
- }
- returntrue;
- }
- ///<summary>
- ///释放初始化程序运行标志,如果释放失败将抛出异常
- ///</summary>
- publicstaticvoidDisposeRunFlag()
- {
- if(File.Exists(RunFlag))
- {
- File.Delete(RunFlag);
- }
- }
以上介绍C# SingleInstance类
【编辑推荐】