zoukankan      html  css  js  c++  java
  • 防止再次运行同一个应用程序

          防止同时打开多个应用程序的实例,也就是同时只有一个程序在运行,再运行同一个程序时,会自动关闭,以下代码放在项目的.dpr(项目文件)中的Application.Initialize;上面。

          代码如下
    var
        hMutex: DWORD;
        Ret: Integer;
    {以上代码要放在begin..end上}

    hMutex :
    = 0;
        
    try
            hMutex :
    = CreateMutex(nil, False, 'eShapeOfClient'); //TRUE 标明该进程拥有此 Mutex 对象eShapeOfClient为自己的程序文件名或自己定义的任意名称
            Ret := GetLastError;
            
    if (Ret = ERROR_ALREADY_EXISTS) then //Mutex 对象是否存在
            begin
                ReleaseMutex(hMutex);
                Application.Terminate; 
    //退出程序
            end
            
    else if hMutex = ERROR_INVALID_HANDLE then
            begin
                InfoDlg(
    '此程序正在运行,关闭后请重新再试!');  //InfoDlg函数是自己定义的,通常用MessgeBox来代替
                Application.Terminate;;
            end;
        
    finally
            ReleaseMutex(hMutex);
        end;

          以上代码没有任何问题,但在运行第二个程序关闭时,也就是Application.Terminate; 会有屏闪,如果改成Exit;好像没有问题。
  • 相关阅读:
    python处理孤立的异常点
    使用redis实现程序或者服务的高可用
    redis报错: redis.exceptions.ResponseError: value is not an integer or out of range
    angular6 使用信息提示框toast
    浏览器中模仿跨域请求
    python aes_cbc加密
    openresty钉钉免密登陆
    openresty 钉钉签名计算
    ansible服务部署
    tornado 文件上传
  • 原文地址:https://www.cnblogs.com/sonicit/p/827382.html
Copyright © 2011-2022 走看看