AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi CreateSemaphore "systemweiter Counter"
Thema durchsuchen
Ansicht
Themen-Optionen

CreateSemaphore "systemweiter Counter"

Ein Thema von Tim Henford · begonnen am 16. Sep 2008 · letzter Beitrag vom 16. Sep 2008
Antwort Antwort
Tim Henford

Registriert seit: 14. Sep 2006
169 Beiträge
 
#1

CreateSemaphore "systemweiter Counter"

  Alt 16. Sep 2008, 08:52
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
  Mit Zitat antworten Zitat
Basilikum

Registriert seit: 9. Aug 2003
389 Beiträge
 
Delphi 7 Professional
 
#2

Re: CreateSemaphore "systemweiter Counter"

  Alt 16. Sep 2008, 17:17
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...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:59 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