只允许应用程序有 3 个使用信号量的实例

发布于 2024-12-05 07:37:46 字数 316 浏览 1 评论 0原文

我正在尝试使用信号量实现一个简单的例程,该例程允许我仅运行该应用程序的 3 个实例。我可以使用 3 个互斥体,但这不是一个好方法,到目前为止我已经尝试过,

var
  hSem:THandle;
begin
  hSem := CreateSemaphore(nil,3,3,'MySemp3');
  if hSem = 0 then
  begin
    ShowMessage('Application can be run only 3 times at once');
    Halt(1);
  end;

我怎样才能正确地做到这一点?

I am trying to implement a simple routine using semaphores that will allow me to run only 3 instances of the application. I could use 3 mutexes but that is not a nice approach i tried this so far

var
  hSem:THandle;
begin
  hSem := CreateSemaphore(nil,3,3,'MySemp3');
  if hSem = 0 then
  begin
    ShowMessage('Application can be run only 3 times at once');
    Halt(1);
  end;

How can i do this properly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终确保释放信号量,因为如果应用程序终止,这不会自动完成。

program Project72;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

var
  hSem: THandle;

begin
  try
    hSem := CreateSemaphore(nil, 3, 3, 'C15F8F92-2620-4F3C-B8EA-A27285F870DC/myApp');
    Win32Check(hSem <> 0);
    try
      if WaitForSingleObject(hSem, 0) <> WAIT_OBJECT_0 then
        Writeln('Cannot execute, 3 instances already running')
      else begin
        try
          // place your code here
          Writeln('Running, press Enter to stop ...');
          Readln;
        finally ReleaseSemaphore(hSem, 1, nil); end;
      end;
    finally CloseHandle(hSem); end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Always make sure that you release a semaphore because this is not done automatically if your application dies.

program Project72;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;

var
  hSem: THandle;

begin
  try
    hSem := CreateSemaphore(nil, 3, 3, 'C15F8F92-2620-4F3C-B8EA-A27285F870DC/myApp');
    Win32Check(hSem <> 0);
    try
      if WaitForSingleObject(hSem, 0) <> WAIT_OBJECT_0 then
        Writeln('Cannot execute, 3 instances already running')
      else begin
        try
          // place your code here
          Writeln('Running, press Enter to stop ...');
          Readln;
        finally ReleaseSemaphore(hSem, 1, nil); end;
      end;
    finally CloseHandle(hSem); end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
初见终念 2024-12-12 07:37:46

您可以使用 JCL

You could use TJclAppInstances from the JCL.

音盲 2024-12-12 07:37:46
  1. 你必须尝试看看它是否被创建
  2. 你必须使用其中一个等待函数来看看你是否可以得到一个计数
  3. 最后你必须释放锁并释放它。处理,以便下次用户关闭和打开您的应用程序时它可以工作

干杯

var
  hSem: THandle;
begin
  hSem := OpenSemaphore(nil, SEMAPHORE_ALL_ACCESS, True, 'MySemp3');
  if hSem = 0 then
    hSem := CreateSemaphore(nil, 3, 3,'MySemp3');

  if hSem = 0 then
  begin
    ShowMessage('... show the error');
    Halt(1);
    Exit;     
  end;

  if WaitForSingleObject(hSem, 0) <> WAIT_OBJECT_0 then
  begin
    CloseHandle(hSem);
    ShowMessage('Application can be runed only 3 times at once');
    Halt(1);
    Exit; 
  end;

  try   
    your application.run codes..

  finally
    ReleaseSemaphore(hSem, 1, nil);
    CloseHandle(hSem);
  end; 
  1. You must try to see if it was created
  2. You must use one of the wait function to see if you can get a count
  3. At the end, you must release the lock & handle so that it can work the next time user close and open your app

Cheers

var
  hSem: THandle;
begin
  hSem := OpenSemaphore(nil, SEMAPHORE_ALL_ACCESS, True, 'MySemp3');
  if hSem = 0 then
    hSem := CreateSemaphore(nil, 3, 3,'MySemp3');

  if hSem = 0 then
  begin
    ShowMessage('... show the error');
    Halt(1);
    Exit;     
  end;

  if WaitForSingleObject(hSem, 0) <> WAIT_OBJECT_0 then
  begin
    CloseHandle(hSem);
    ShowMessage('Application can be runed only 3 times at once');
    Halt(1);
    Exit; 
  end;

  try   
    your application.run codes..

  finally
    ReleaseSemaphore(hSem, 1, nil);
    CloseHandle(hSem);
  end; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文