AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Mutex Synchronisation funktioniert nicht?!

Mutex Synchronisation funktioniert nicht?!

Ein Thema von stoxx · begonnen am 13. Jan 2014 · letzter Beitrag vom 13. Jan 2014
Antwort Antwort
Benutzerbild von stoxx
stoxx

Registriert seit: 13. Aug 2003
1.111 Beiträge
 
#1

Mutex Synchronisation funktioniert nicht?!

  Alt 13. Jan 2014, 20:52
ich möchte zwei Programme mit einem Mutex synchronisieren.
Irgendwie geht das aber nicht. Der eine Button ist dazu da, einfach in einer while Schleife für 15 Sekunden zu "hängen"
in der Zeit drückt man in der zweiten gestarteten Exe den anderen TestButton (btnSynCheckClick) , der ja dann warten müsste.
Diese Anwendung hängt sich aber für ewig auf, und kommt bei WaitforsingleObject nie wieder ...

Was mach ich falsch?

Delphi-Quellcode:
unit ufrm_Mutex;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TfrmMutex = class(TForm)
    btnThreadLoop: TButton;
    btnSynCheck: TButton;
    Memo1: TMemo;
    procedure btnThreadLoopClick(Sender: TObject);
    procedure btnSynCheckClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    Mutex : THandle;
  end;

var
  frmMutex: TfrmMutex;

implementation

{$R *.dfm}

//==============================================================================

procedure TfrmMutex.btnSynCheckClick(Sender: TObject);
begin
    Memo1.Lines.add('Before Enter');
    if WaitForSingleObject(Mutex, INFINITE) <> WAIT_OBJECT_0 then
        RaiseLastOSError;
     try
     finally
       ReleaseMutex(Mutex)
     end;
     Memo1.Lines.add('After Enter');
end;

//==============================================================================

procedure TfrmMutex.btnThreadLoopClick(Sender: TObject);
var
  tout : Cardinal;
begin

    if WaitForSingleObject(Mutex, INFINITE) <> WAIT_OBJECT_0 then
        RaiseLastOSError;
     try
         Memo1.Lines.add('Loop Begin');
         tout := GetTickCount + 15000;
         while GetTickCount < tOut do begin
            sleep(1);
         end; // while

     finally
       ReleaseMutex(Mutex)
     end;
     Memo1.Lines.add('Loop End');

end;

//==============================================================================

procedure TfrmMutex.FormCreate(Sender: TObject);
var
  FSA: SECURITY_ATTRIBUTES;
  FSD: SECURITY_DESCRIPTOR;
begin
  InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
  SetSecurityDescriptorDacl(@FSD, true, nil, false);
  FSA.lpSecurityDescriptor := @FSD;
  FSA.nLength := sizeof(SECURITY_ATTRIBUTES);
  FSA.bInheritHandle := false;

  Mutex := CreateMutex(@FSA, true, PAnsiChar('Mutex1') );
  Memo1.Lines.add('MutexHandle: ' + inttostr(Mutex) );

end;
//==============================================================================

procedure TfrmMutex.FormDestroy(Sender: TObject);
begin
  CloseHandle(Mutex);
end;

end.
Phantasie ist etwas, was sich manche Leute gar nicht vorstellen können.
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#2

AW: Mutex Synchronisation funktioniert nicht?!

  Alt 13. Jan 2014, 21:30
Setz mal den bInitialOwner Parameter bei CreateMutex auf false, dann geht es

Edit mit Quote aus dem MSDN:
Zitat:
If this value is TRUE and the caller created the mutex, the calling thread obtains initial ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
Zitat:
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes with sufficient access rights simply open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)

Geändert von Zacherl (13. Jan 2014 um 21:33 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von stoxx
stoxx

Registriert seit: 13. Aug 2003
1.111 Beiträge
 
#3

AW: Mutex Synchronisation funktioniert nicht?!

  Alt 13. Jan 2014, 21:35
Setz mal den bInitialOwner Parameter bei CreateMutex auf false, dann geht es
das geht jetzt wirklich .. nur .. ich könnte schwören, ich hätte das schon probiert .. (natürlich gleich als erstes)
(vielleicht war der Tag wohl doch schon etwas lang? )
..

ähm .. danke erstmal .. ich werde nochmal bissl rumtesten
Phantasie ist etwas, was sich manche Leute gar nicht vorstellen können.
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 21:07 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