当前位置: 移动技术网 > IT编程>开发语言>Delphi > Delphi - 互斥对象下实现系统的单例模式

Delphi - 互斥对象下实现系统的单例模式

2019年09月03日  | 移动技术网IT编程  | 我要评论

使用createmutex函数创建互斥对象

利用windows系统函数createmutex(),找出当前系统是否已经存在指定进程的实例,如果没有则创建一个互斥体。

createmutex函数原型如下:

function createmutex(lpmutexattributes: psecurityattributes; binitialowner: bool; lpname: pchar): thandle; 

其中参数:

lpmutexattributes :security_attributes 结构类型指针,可以为null。

binitialowner :是否初始化互斥体。

lpname :互斥体对象的名称,一般是工程的名称。

最终,函数返回一个互斥体句柄。

 

delphi winfrm利用互斥对象实现单例模式

单击project,view source;

uses 中添加windows;

begin和end之间添加如下代码。

 

 

  //*****************************单例模式********************************
  createmutex(nil, false, 'application name');
  if getlasterror = error_already_exists then
  begin
    application.messagebox('系统已经开启了,请确认下!', '提示', mb_ok);
    halt(0);
  end;
    //*****************************单例模式********************************

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网