Winform应用程序的单一例程

前端之家收集整理的这篇文章主要介绍了Winform应用程序的单一例程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在做WinFrom开发的时候,很多时候我们想只有一个例程(routine)在运行。就像是设计模式中的Single Pattern,其原理大致相同。

那么我们看一下Single Pattern的实现原理。

 
 
  1. @H_301_8@public@H_301_8@classSinglePattern
  2. {
  3. @H_301_8@private@H_301_8@staticSinglePatterninstance=@H_301_8@null;
  4. @H_301_8@public@H_301_8@staticSinglePatternInstance()
  5. {
  6. @H_301_8@if(instance==@H_301_8@null)
  7. {
  8. instance=@H_301_8@newSinglePattern();
  9. }
  10. @H_301_8@returninstance;
  11. }
  12. }

在这样的一个最基本的单例模式中,使用了静态变量instance来保存SinglePattern的实例。无论如何在我们的应用程序中都只有一个分instance,并且以此确定了SinglePattern.Instance()方法返回的实例是单一实例。

那么我们又怎么在WinForm中,给Form创建单一的窗口呢?

比葫芦画瓢,我们需要寻找一个跟静态变量类似的东西来确认只有一份存在。

  1. 文件系统中的文件,统一路径下没有重名的文件(包含扩展名)。
  2. 多线程中的Mutex,在操作系统级别也是唯一的。

如何用文件实现WinFrom的单例在这儿就不多说了。举例说一下如何使用Mutex实现WinForm的单例。

 
 
  1. @H_301_8@boolisNew=@H_301_8@false;
  2. Mutexmutext=@H_301_8@newMutex(@H_301_8@true,"testFrom",@H_301_8@outisNew);

当名字为testForm的Mutext存在时,isNew为False,否则为True。现在看来实现单例的WinFrom就有了理论依据。但是总部能再WinForm的构造函数中写这样的判断吧,这样只是完成了一个窗口的单例。我们现在想要是应用程序级别的单例。从何处下手呢?

不要着急Main()函数是所有应用程序的入口函数。这要在这里加入判断就行了。

 
 
  1. @H_301_8@usingSystem;
  2. @H_301_8@usingSystem.Collections.Generic;
  3. @H_301_8@usingSystem.Linq;
  4. @H_301_8@usingSystem.Windows.Forms;
  5. @H_301_8@usingSystem.Runtime.InteropServices;
  6. @H_301_8@usingSystem.Threading;
  7. @H_301_8@usingSystem.Diagnostics;
  8. @H_301_8@namespaceYoung.Winfrom.Singleton
  9. {
  10. @H_301_8@static@H_301_8@classProgram
  11. {
  12. //把句柄hWnd设置为以cmdShow的模式显示出来
  13. [DllImport("user32.dll",EntryPoint="ShowWindowAsync")]
  14. @H_301_8@public@H_301_8@static@H_301_8@extern@H_301_8@intShowWindowAsync(IntPtrhWnd,@H_301_8@intcmdShow);
  15. //把句柄hWnd放置到所有窗口的最前端
  16. [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
  17. @H_301_8@public@H_301_8@static@H_301_8@extern@H_301_8@intSetForegroundWindow(IntPtrhWnd);
  18. @H_301_8@staticMutexme;
  19. @H_301_8@private@H_301_8@const@H_301_8@intSHOWNORMAL=1;//正常显示
  20. @H_301_8@private@H_301_8@const@H_301_8@intCLOSE=0;//关闭
  21. @H_301_8@private@H_301_8@const@H_301_8@intMINISIZE=2;//最小化
  22. @H_301_8@private@H_301_8@const@H_301_8@intMAXSIZE=3;//最大化
  23. ///<summary>
  24. ///应用程序的主入口点。
  25. ///</summary>
  26. [STAThread]
  27. @H_301_8@static@H_301_8@voidMain()
  28. {
  29. @H_301_8@boolisNew=@H_301_8@false;
  30. me=@H_301_8@newMutex(@H_301_8@true,@H_301_8@outisNew);
  31. @H_301_8@if(!isNew)
  32. {
  33. Processpro=GetProcess();
  34. @H_301_8@if(pro!=@H_301_8@null)
  35. {
  36. IntPtrfromHandle=pro.MainWindowHandle;
  37. ShowWindowAsync(fromHandle,SHOWNORMAL);
  38. SetForegroundWindow(fromHandle);
  39. }
  40. @H_301_8@return;
  41. }
  42. @H_301_8@else
  43. {
  44. Application.EnableVisualStyles();
  45. Application.SetCompatibleTextRenderingDefault(@H_301_8@false);
  46. Application.Run(@H_301_8@newForm1());
  47. }
  48. }
  49. @H_301_8@staticProcessGetProcess()
  50. {
  51. Processpro=Process.GetCurrentProcess();
  52. @H_301_8@stringcurrent=pro.MainModule.FileName;
  53. Process[]pros=Process.GetProcessesByName(pro.ProcessName);
  54. @H_301_8@intl=pros.Length;
  55. @H_301_8@foreach(Processp@H_301_8@inpros)
  56. {
  57. @H_301_8@if(p.MainModule.FileName.Equals(current,StringComparison.CurrentCultureIgnoreCase))
  58. {
  59. @H_301_8@if(p.Id!=pro.Id)
  60. @H_301_8@returnp;
  61. }
  62. }
  63. @H_301_8@return@H_301_8@null;
  64. }
  65. }
  66. }

上边的例子中使用了Windows的API,激活已经启动例程并将窗口移动到最顶端。

猜你在找的设计模式相关文章