WPF 同一个程序 只允许 同时运行一个



方法2 当程序已经运行了 再运行这个程序时,则显示当前这个窗体 http://code.3rbang.com/cshape-run-one/ VS2013附件:http://files.cnblogs.com/xe2011/WpfApplication_System.Threading.Mutex.rar .NET 已经提供一个 类System.Threading.Mutex 所以不采用APICreateMutexApp.xaml.cs 中实现代码,重这写OnStartup usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.Data; usingSystem.Windows; namespaceWpfApplication7 { ///<summary> ///App.xaml的交互逻辑 ///</summary> publicpartialclassApp:Application { #regionDllImport... [System.Runtime.InteropServices.DllImport("User32.dll")] privatestaticexternboolSetForegroundWindow(IntPtrhWnd); [System.Runtime.InteropServices.DllImport("User32.dll")] privatestaticexternboolShowWindowAsync(IntPtrhWnd,intcmdShow); privateconstintSW_SHOW=1; #endregion boolcreatedNew; protectedoverridevoidOnStartup(StartupEventArgse) { System.Threading.Mutexmutex=newSystem.Threading.Mutex(true,"HelloRoman",outcreatedNew); if(!createdNew) { System.Diagnostics.Processprogress1=GetExistProcess(); if(progress1!=null) { ShowMainWindow(progress1); Environment.Exit(0); } } } ///<summary> ///最前端显示主窗体 ///</summary> ///<paramname="process"></param> privatevoidShowMainWindow(System.Diagnostics.Processprocess) { IntPtrmainWindowHandle1=process.MainWindowHandle; if(mainWindowHandle1!=IntPtr.Zero) { ShowWindowAsync(mainWindowHandle1,SW_SHOW); SetForegroundWindow(mainWindowHandle1); } } ///<summary> ///查看程序是否已经运行 ///</summary> ///<returns></returns> privatestaticSystem.Diagnostics.ProcessGetExistProcess() { System.Diagnostics.ProcesscurrentProcess=System.Diagnostics.Process.GetCurrentProcess(); foreach(System.Diagnostics.Processprocess1inSystem.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName)) { if((process1.Id!=currentProcess.Id)&& (System.Reflection.Assembly.GetExecutingAssembly().Location==currentProcess.MainModule.FileName)) { returnprocess1; } } returnnull; } } }
方法1 usingSystem; usingSystem.Collections.Generic; usingSystem.Configuration; usingSystem.Data; usingSystem.Linq; usingSystem.Windows; namespaceWpfApplication6 { ///<summary> ///App.xaml的交互逻辑 ///</summary> publicpartialclassApp:Application { [System.Runtime.InteropServices.DllImport("kernel32.dll",EntryPoint="CreateMutex")] publicstaticexternIntPtrCreateMutex(intlpSecurityAttributes,boolbInitialOwner,stringlpName); [System.Runtime.InteropServices.DllImport("kernel32.dll",EntryPoint="GetLastError")] publicstaticexternintGetLastError(); privateconstintERROR_ALREADY_EXISTS=183; protectedoverridevoidOnStartup(StartupEventArgse) { stringappTitle="2014年5月31日7:28:29"; IntPtrhMutex=CreateMutex(0,true,appTitle); if(GetLastError()==ERROR_ALREADY_EXISTS) { MessageBox.Show("程序已经运行!"); Application.Current.Shutdown(); } } } }



来自为知笔记(Wiz)



优质内容筛选与推荐>>
1、android之Intent小结
2、[LeetCode] String to Integer (atoi)
3、vue学习链接
4、VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程
5、[转]MySQL的limit查询优化


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号