Clicky

My familarity with Pascal is essentially Borland.  

At present I am writing an application that uses Free Pascal for my own use.  

I have used the TProcess to open a graphic viewer and text editor at the same time.  The application won't go anywhere until these are both closed.  Then after more processing it goes back into the loop and opens a new graphic and text document.  It works OK but I'd like some comments if this is the correct way to do it and if it can be improved.

For example I have used Sleep(1).  Could that be changed to other values?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var
FirstProcess, SecondProcess: TProcess;	

repeat loop start

FirstProcess  := TProcess.Create(nil);
SecondProcess := TProcess.Create(nil);
FirstProcess.CommandLine  := path_to_graphic_viewer_with_file_name;
SecondProcess.CommandLine := path_to_text_editor_with_file_name;    
FirstProcess.Execute;
SecondProcess.Execute;
while (FirstProcess.Running or SecondProcess.Running) do
begin          
	Sleep(1); 
end;
FirstProcess.Free;
SecondProcess.Free;

until list_of_files_processed

asked 06/30/2011 04:43

dbrunton's gravatar image

dbrunton ♦♦


5 Answers:
To get waiting sytem messages handled, you can put APPLICATION.PROCESSMESSAGES into your loop...
link
Thommy's gravatar image

Thommy

assuming that your OS is windows AND your app is a GUI app, otherwise the following does not make much sense.

I would put the create process / wait for completion / loop in a separate thread, so user interface keeps responding.

something like the following:

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:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
type
  TMyThread = class(TThread)
  protected
    FStop: Thandle;
    procedure Execute; override;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
    procedure Stop;
  end;

{ MyThread }

constructor TMyThread.Create;
begin
  FStop := CreateEvent(nil, False, False, nil);
  inherited Create(False);
end;

destructor TMyThread.Destroy;
begin
  Stop;
  CloseHandle(FStop);
  inherited;
end;

procedure TMyThread.Execute;
var
  bDone1, bDone2: Boolean;
  handles: array[0..2] of THandle;
begin
  repeat
    FirstProcess  := TProcess.Create(nil);
    SecondProcess := TProcess.Create(nil);
    try
      FirstProcess.CommandLine  := path_to_graphic_viewer_with_file_name;
      SecondProcess.CommandLine := path_to_text_editor_with_file_name;
      FirstProcess.Execute;
      SecondProcess.Execute;
      //array of objects we are waiting on
      handles[0] := FirstProcess.Handle;
      handles[1] := SecondProcess.Handle;
      handles[2] := FStop;
      bDone1 := False;
      bDone2 := False;
      while (True) do begin
        case WaitForMultipleObjects(3, @handles, False, INFINITE) of
          WAIT_OBJECT_0 + 0: begin
            //first process done
            if bDone2 then
              Break; //break if also the second done
            bDone1 := True;
          end;

          WAIT_OBJECT_0 + 1: begin
            //second process done
            if bDone1 then
              Break; //break if also the first done
            bDone2 := True;
          end;

          WAIT_OBJECT_0 + 2: begin
            //user break
            Exit;
          end;
        end;
      end;
    finally
      FirstProcess.Free;
      SecondProcess.Free;
    end;
  until list_of_files_processed;
end;

procedure TMyThread.Stop;
begin
  SetEvent(FStop);
  WaitFor;
end;


usage:
procedure Form.Button1Click;
begin
  thread := TMyThread.Create;
end;

procedure Form.OnDestroy
begin
  thread.Free; // this will call Stop, Wait for thread to finish and free resources
end;
link
lomo74's gravatar image

lomo74

That multi-threading code is great lomo,
c expert to delphi?
I wonder Geert has not comment yet.
link
systan's gravatar image

systan

@systan:
thank you :-)
I like playing with multi-threading toys ;-)
link
lomo74's gravatar image

lomo74

Closing down question.

Application wasn't really a GUI then.  (Free Pacal)

However it is one now (Lazarus) and the TProcess code I posted earlier isn't really relevant to the Lazarus port.

The code by lomo74 looks interesting and might prove useful in future.

Thanks for comments.
link
dbrunton's gravatar image

dbrunton

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×2
×1

Asked: 06/30/2011 04:43

Seen: 298 times

Last updated: 07/07/2011 11:24