Home > Back-end >  Delphi how to implement the main. Exe monitoring sub. Exe?
Delphi how to implement the main. Exe monitoring sub. Exe?

Time:11-18

Main purpose is, sub. Exe once closed, the main the. Exe followed was shut down,

I put this into the timer, has been in the loop, the feeling is wrong, if there is a more advanced method?

In the main. Exe in writing, if no sub in the process. The exe is ExitProcess (0);

CodePudding user response:

The simplest use timer, but need not cycle? Set time shorter and then monitoring sub. Exe process handle is ok, once get failed to quit,

CodePudding user response:

If the Main and Sub are all written by yourself, it is very simple, you make Sub exit when send a message to one of the Main window,
If Main oneself are written, and Sub is not write their own, then the above practice
If the Main is not written and Sub is yourself, then end of the Sub FindWindow first to find out the Main the Main window to send a WM_CLOSE message in the past

CodePudding user response:

SUB. Exe program:
When a call Main. EXE, write down its handle,
ExeHandle:=findWindow (nil, 'window name);//returns a handle to an EXE
Or
Shellexecute (exehandle, 'open', pchar (' main. Exe), ' ', nil, sw_shownormal);

Two, exit, close the handle to the thread,
If ExeHandle<> 0 then
The begin
SendMessage (EXEHandle WM_Close, 0, 0);//send EXE closed message
ExeHandle:=0;
end;

CodePudding user response:

If are all written by DELPHI has a very simple solution, direct write a callback function (the callback)

1. MainForm. Write a
Procedure MainForm. CallBackClose (Sender: TOBject);
The begin
Try
Self. Free
Except,
end;
end;
2. The subForm to callback code
Increase the callback function type
Type
TCallBackClose: procedure (Sender: TOBject) of the object;
.
Var
CallBackMain: TCallBackClose;
.
SubForm before the close of the event to add
CallBackMain (SELF);
3. MainForm start SUBFORM down HANDLE
4. Find SUBFORM through HANDLE
5. Pass the MainForm CallBackClose SUBFORM
TForm (subFormHWND). CallBackMain:=CallBackClose;

So, do not need to thread, do not need to message, do not need the TIMER to listen, as long as the SUBFORM CLOSE or you want to CLOSE MainForm, perform CallBackMain MainForm is CLOSE (SELF)
  • Related