Autor Beitrag
stiftmaster
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 149



BeitragVerfasst: So 22.01.06 15:25 
Hallo Leute,

ich habe mir ein Konsolenprogramm geschrieben. Dieses wird von meinem Rechner jede Min. ausgeführt. Da in dem Programm eine Internetverbindung benötigt wird und hin und wieder die Leitung überlastet ist, kann es sein, dass der Vorgang länger als eine Min. dauert und dadurch die Konsole zwei Mal läuft - und das darf sie nicht :-)

Ich habe erstmal mit Mutex mein Glück versucht:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
unit MutexConfig;

interface

implementation

uses Windows, Dialogs, Sysutils, Forms, Classes;

var
  Mutex: THandle;
  h: HWND;

initialization

  Mutex := CreateMutex(nil, True, 'LF_Dakota_App');
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    h := 0;
    repeat
      h := FindWindowEx(0, h, 'TApplication', PChar(Application.Title));
    until h <> Application.Handle;
    if h <> 0 then
    begin
      Windows.ShowWindow(h, SW_SHOWNORMAL);
      Windows.SetForegroundWindow(h);
    end;
  Halt;
  end;

finalization

  ReleaseMutex(Mutex);

end.


Nachdem das nicht lief, hab ich mal geschaut, woran es liegt: until h <> Application.Handle. Eine Konsole hat kein Handle und daher klappt das ganze nicht. Leider weiß ich nicht, wie ich das Problem jetzt in den Griff kriegen soll...
a) die Konsole bekommt ein Handle (keine Ahnung wie das gehen soll)
b) n' andere Möglichkeit muss her.

Hat jemand von euch eine Idee - bin für jeden Tipp dankbar.

Dankend

Stefan
stiftmaster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 149



BeitragVerfasst: So 22.01.06 17:03 
Hi,

nachdem ich noch weiter im Netz gesucht habe, bin ich auf einen viel einfacheren Code gestoßen, der seinen Dienst perfekt tut :-)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
unit MutexConfig;

interface

uses
  Windows;

implementation

var mHandle: THandle;
 
initialization   
  mHandle := CreateMutex(nil, True, 'LF_Dakota_App');
  if (GetLastError = ERROR_ALREADY_EXISTS) then
    halt;

finalization
  if mHandle <> 0 then
    CloseHandle(mHandle);
end.


Bis dann

Stefan
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Mi 25.01.06 10:22 
Dazu hätte auch ein Blick in die Delphi-Library gereicht. ;-)

...verhindern dass die Anwendung mehrmals gestartet wird?

_________________
Ein Nutzer der Ecke ;-)