程序如果运行好几次开几个的话,操作数据库就会有问题,如果限制这个进程在同一时间只能有1个??有没有相关的api函数,或者是其他什么好方法

解决方案 »

  1.   

    用CreateMutex函数,你搜索一下,会有很多例子的
      

  2.   

    program IhomeOA;uses
      Forms,
      Windows,
      Controls,
      U_main in 'U_main.pas' {Frmmain},
      U_login in 'U_login.pas' {Frmlogin},
      Cdm in 'Cdm.pas' {Dm: TDataModule},
      CSysSetup in 'CSysSetup.pas' {fmSysSetup},
      U_openproject in 'U_openproject.pas' {Frmopenproject},
      U_product in 'U_product.pas' {Frmproduct},
      U_provider in 'U_provider.pas' {Frmprovider},
      U_choosepart in 'U_choosepart.pas' {frmchoosepart},
      U_chooseproduct in 'U_chooseproduct.pas' {frmchooseproduct},
      U_Customers in 'U_Customers.pas' {frmCustomers},
      U_payment in 'U_payment.pas' {Frmpayment},
      U_selectpart in 'U_selectpart.pas' {frmselectpart},
      U_changepassword in 'U_changepassword.pas' {Frmchangepassword},
      U_stockmanage in 'U_stockmanage.pas' {frmstockmanage},
      U_help in 'U_help.pas' {frmhelp},
      U_select in 'U_select.pas' {Frmselect},
      U_printstock in 'U_printstock.pas' {Frmprintstock},
      U_users in 'U_users.pas' {frmUsers},
      U_newproject in 'U_newproject.pas' {Frmnewproject},
      U_selectcp in 'U_selectcp.pas' {Frmselectcp},
      U_createorder in 'U_createorder.pas' {Frmcreateorder},
      U_modifynum in 'U_modifynum.pas' {Frmmodifynum},
      U_selectsupplier in 'U_selectsupplier.pas' {frmselectsupplier},
      U_stoctinout in 'U_stoctinout.pas' {Frmstockinout},
      U_stockselect in 'U_stockselect.pas' {Frmstockselect},
      U_shouldpay in 'U_shouldpay.pas' {Frmshouldpay},
      U_projectsel in 'U_projectsel.pas' {Frmprojectsel},
      U_receive in 'U_receive.pas' {frmreceive},
      U_proselect in 'U_proselect.pas' {frmproselect},
      U_payprint in 'U_payprint.pas' {Frmpayprint},
      U_receiveprint in 'U_receiveprint.pas' {frmreceiveprint},
      U_balance in 'U_balance.pas' {frmbalance},
      U_balanceprint in 'U_balanceprint.pas' {frmbalanceprint},
      U_printorder in 'U_printorder.pas' {frmprintorder},
      U_balancetotalprint in 'U_balancetotalprint.pas' {frmbalancetotalprint},
      U_deleteproject in 'U_deleteproject.pas' {frmdeleteproject};{$R *.res}
    var
      Hmutex :HWND ;
      Ret : Integer ;begin
      Application.Initialize;
      Hmutex := CreateMutex(nil,False,'IhomeOA');
      Ret := GetLastError;
       if Ret <> ERROR_ALREADY_EXISTS then
      begin
      Application.CreateForm(TDm, Dm);
      Application.CreateForm(TFrmmain, Frmmain);
      Application.CreateForm(Tfrmchoosepart, frmchoosepart);
      Application.CreateForm(Tfrmchooseproduct, frmchooseproduct);
      Application.CreateForm(TFrmpayment, Frmpayment);
      Application.CreateForm(Tfrmselectpart, frmselectpart);
      Application.CreateForm(TFrmchangepassword, Frmchangepassword);
      Application.CreateForm(Tfrmhelp, frmhelp);
      Application.CreateForm(TFrmselect, Frmselect);
      Application.CreateForm(TFrmprintstock, Frmprintstock);
      Application.CreateForm(TFrmselectcp, Frmselectcp);
      Application.CreateForm(TFrmmodifynum, Frmmodifynum);
      Application.CreateForm(Tfrmselectsupplier, frmselectsupplier);
      Application.CreateForm(TFrmstockselect, Frmstockselect);
      Application.CreateForm(TFrmprojectsel, Frmprojectsel);
      Application.CreateForm(Tfrmproselect, frmproselect);
      Application.CreateForm(TFrmpayprint, Frmpayprint);
      Application.CreateForm(Tfrmreceiveprint, frmreceiveprint);
      Application.CreateForm(Tfrmbalance, frmbalance);
      Application.CreateForm(Tfrmbalanceprint, frmbalanceprint);
      Application.CreateForm(Tfrmprintorder, frmprintorder);
      Application.CreateForm(Tfrmbalancetotalprint, frmbalancetotalprint);
      Frmlogin:= TFrmlogin.Create(Application);
        if Frmlogin.ShowModal = mrOk then
        begin
         Application.CreateForm(TFrmmain,frmmain);
         Application.Run;
        end
        else
        begin
         Application.ShowMainForm := False;
         Application.Terminate;
         Application.Run;
        end;
      end
      else begin
        ReleaseMutex(Hmutex);
        Application.MessageBox('程序已经启动,不能同时启动多个程序','启动程序',MB_OK+MB_ICONINFORMATION);
      end;
      end.