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/)
-   -   Mutex Synchronisation funktioniert nicht?! (https://www.delphipraxis.net/178543-mutex-synchronisation-funktioniert-nicht.html)

stoxx 13. Jan 2014 20:52

Mutex Synchronisation funktioniert nicht?!
 
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.

Zacherl 13. Jan 2014 21:30

AW: Mutex Synchronisation funktioniert nicht?!
 
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.

stoxx 13. Jan 2014 21:35

AW: Mutex Synchronisation funktioniert nicht?!
 
Zitat:

Zitat von Zacherl (Beitrag 1243542)
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 :?


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:19 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