Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi CreateSemaphore "systemweiter Counter" (https://www.delphipraxis.net/120749-createsemaphore-systemweiter-counter.html)

Tim Henford 16. Sep 2008 08:52


CreateSemaphore "systemweiter Counter"
 
Hallo,

also folgendes Problem:
Ich habe mehrere Programme, die die gleiche DLL-Funktion nutzen. Diese Funktion soll aber nur einmal laufen.
Da dachte ich, ich kann in der DLL-Funktion mit CreateSemaphore ein Semaphore "registrieren", dass von der anderen Anwendungen mit "OpenSemaphore" erkannt wird und sobald die Dll-Funktion beendet ist, soll das Semaphore wieder entfernt werden, dass scheint aber nicht zu funktionieren.

Woran liegt das?


Delphi-Quellcode:
const
  SEMAPHORE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or 3;

var
  Form1: TForm1;
  Semaphore: THandle;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Semaphore:= CreateSemaphore(nil, 0,1,'TestName');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  { When calling OpenSemaphore, you use the first parameter to identify the
    type of access that you want for the semaphore. Most developers pass the
    value SEMAPHORE_ALL_ACCESS.

    Use the 2nd parameter to identify whether or not a process created by this
    process can inherit the handle to the mutex. If you pass True, a process
    created through a call to CreateProcess can inherit the mutex handle,
    otherwise it cannot.

    The 3rd parameter is the name of the semaphore. As is the case with mutexes,
    this name must be unique.

    If OpenSemaphore is successful, it returns the handle of the semaphore.
    If OpenSemaphore fails, it returns 0.
    If OpenSemaphore returns 0, use GetLastError to determine the cause of the failure. }
  if OpenSemaphore(SEMAPHORE_ALL_ACCESS, true,'TestName')<>0 then
    ShowMessage ('Schon offen');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Semaphore<>0 then begin
    ShowMessage ('Rel.');
    { The 1st argument is the handle to the semaphore, and
      the 2nd is the number of counts to add to the semaphore. A thread should
      only increment the semaphore's count equal to the number of counts held
      by that thread. Normally, this will be equal to 1.

      The 3rd parameter is a pointer to 32-bit variable that can be assigned
      the count of the semaphore prior to the release. If you do not need to
      know the semaphore's previous count, pass nil in this third parameter.

      All of the semaphore related functions and constants discussed in this
      section are declared in the Windows unit. Consequently, this unit must
      appear in the uses clause of any code that needs to use semaphores. }
    ReleaseSemaphore(Semaphore, 1, @Semaphore);
  end;
end;
Danke Tim

Basilikum 16. Sep 2008 17:17

Re: CreateSemaphore "systemweiter Counter"
 
OpenSemaphore alleine genügt nicht, du musst sie/ihn/es :-) mittels WaitForSingleObject auch noch reservieren... sinnigerweise würdest du dann bei CreateSemaphore den lInitialCount auf 1 setzen... und da es eh ein "Counter" von 0 bis 1 wird, könntest du gleich einen Mutex verwenden (CreateMutex und Konsorten) - ein spezialisierter Semaphore von 0 bis 1...


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz