浅谈如何让Windows Mobile只运行一个实例

移动开发
本文将简单谈谈如何让Windows Mobile只运行一个实例,其中主要用到的互斥的概念。文末作者还提出了自己的一些问题,希望大家积极讨论。

因为一个项目的需求,要实现程序只有一个实例运行。在网上搜了很久,最后在CSDN上面看到一回复。得到启示,完成该功能。

主要用的是互斥对象来实现。代码如下:

  1. staticclassProgram  
  2. {  
  3. [DllImport("coredll.Dll",SetLastError=true)]  
  4. privatestaticexternIntPtrCreateMutex(SECURITY_ATTRIBUTESlpMutexAttributes,boolbInitialOwner,stringlpName);  
  5.  
  6. [DllImport("coredll.Dll",SetLastError=true)]  
  7. privatestaticexternintReleaseMutex(IntPtrhMutex);  
  8.  
  9. [StructLayout(LayoutKind.Sequential)]  
  10. publicclassSECURITY_ATTRIBUTES  
  11. {  
  12. publicintnLength;  
  13. publicintlpSecurityDescriptor;  
  14. publicintbInheritHandle;  
  15. }  
  16.  
  17. constintERROR_ALREADY_EXISTS=0183;  
  18.  
  19. ///<summary> 
  20. ///应用程序的主入口点。  
  21. ///</summary> 
  22. [MTAThread]  
  23. staticvoidMain()  
  24. {  
  25. IntPtrhMutex=CreateMutex(null,false,"StandardWorkMan");  
  26. if(Marshal.GetLastWin32Error()!=ERROR_ALREADY_EXISTS)  
  27. {  
  28. Application.Run(newFormWorkList());  
  29. }  
  30. else  
  31. {  
  32. MessageBox.Show("已经启动了一个程序,请勿重复打开","系统提示",  
  33. MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);  
  34. ReleaseMutex(hMutex);  
  35. Application.Exit();  
  36. }  
  37. }  

上面代码完全正常,我的问题是大部分自己很早就写出来了。只是问题在下面:

  1. [DllImport("coredll.Dll",SetLastError=true)]  
  2. privatestaticexternintGetLastError();  
  3.  
  4. .  
  5.  
  6. if(GetLastError()!=ERROR_ALREADY_EXISTS)  
  7. {  
  8. Application.Run(newFormWorkList());  

我用的是平台调用里面的GetLastError(),结果一直出不来想要的效果。调试时发现无论打开多少个实例,GetLastError()的值一直都是6(INVALID_HANDLE_VALUE)????很不解。望明白的人说明一下。谢谢。

【编辑推荐】

  1. Windows Mobile 6.5将在5月推出
  2. 微软称已完成Windows Mobile6.5开发
  3. 微软开始组织开发大型Windows Mobile应用程序
  4. 微软将推出Windows Mobile应用软件商店
  5. Windows Mobile上实现自动拼写和匹配
责任编辑:彭凡 来源: cnblogs
相关推荐

2009-10-27 09:05:44

Windows 7进程查看

2019-12-11 11:08:51

Windows 10Windows 10

2011-06-09 09:31:40

Qt 实例

2009-09-18 08:58:09

Windows Mob社交平台

2017-11-13 13:33:09

MySQL全备份恢复表

2023-12-18 08:23:12

CSI插件Kubernetes

2012-06-01 10:23:47

Mobile Site优化

2009-12-17 09:09:48

Windows 7系统分区

2022-06-09 07:07:35

服务GPUbatch

2009-07-15 13:41:00

JDBC实例

2011-09-01 10:21:52

jQuery Mobi元素

2022-10-08 00:06:00

JS运行V8

2013-11-18 14:00:09

2022-02-22 20:35:22

公钥私钥数据

2013-08-15 10:00:07

产品产品经理优秀的产品

2011-09-08 13:41:53

Widget

2012-11-20 09:33:02

2009-04-22 08:49:30

Windows Mob微软移动OS

2012-12-27 13:52:48

Android开发判断

2021-02-15 17:29:46

LinuxShell脚本
点赞
收藏

51CTO技术栈公众号