19
Jul 08

ParallelJobs: Simple use

:: articles :: by Gilberto Saraiva

Folks,

This is the first article about ParallelJobs and I’ll show you how to create a simple parallel process.

Lets create a simple drawing system that display a follower effect on a bitmap:

 Delphi |  copy code |? 
01
function TfrmMain.UpdateFollower: integer;
02
const
03
  FOLLOWERSIZE = 30;
04
var
05
  Points: array [1..FOLLOWERSIZE] of TPoint;
06
  i: Integer;
07
begin
08
  while not CurrentJobTerminated do
09
  begin
10
    for i := 1 to FOLLOWERSIZE - 1 do
11
      Points[i] := Points[i + 1];
12
 
13
    with Points[FOLLOWERSIZE], Mouse do
14
    begin
15
      X := CursorPos.X div 3;
16
      Y := CursorPos.Y div 3;
17
    end;
18
 
19
    Bmp.Canvas.Lock;
20
    try
21
      Bmp.Canvas.FillRect(Bmp.Canvas.ClipRect);
22
      for i := 2 to FOLLOWERSIZE do
23
      begin
24
        with Points[i - 1] do
25
          Bmp.Canvas.MoveTo(X, Y);
26
        with Points[i] do
27
          Bmp.Canvas.LineTo(X, Y);
28
      end;
29
    finally
30
      Bmp.Canvas.Unlock;
31
      Invalidate;
32
    end;
33
    Sleep(10);
34
  end;
35
 
36
  Result := 0;
37
end;

This code will hold the last 30 positions(X, Y) of the mouse and when we modify something on Bmp we have to lock the canvas to don’t let it be display by WM_PAINT message on the wrong time.

Since we use the TBitmap Bmp we have to create it and setup as wish. After that its time to create the parallel job with ParallelJobs library.
The code below show how I did it:

 Delphi |  copy code |? 
01
procedure TfrmMain.FormCreate(Sender: TObject);
02
begin
03
  ControlStyle := [csOpaque];
04
  Bmp := TBitmap.Create;
05
  with Bmp do
06
  begin
07
    Width := Screen.Width div 3;
08
    Height := Screen.Height div 3;
09
 
10
    Canvas.Brush.Color := clBlack;
11
    Canvas.Pen.Color := RGB(255, 130, 0);
12
  end;
13
  ParallelJob(Self, @TfrmMain.UpdateFollower);
14
end;

ParallelJobs provide two ways to create a parallel process with any function or method you want, this code use one that you need to specify the object ( Self ) that contain the method and the method to work on a parallel process.

Now we have to create the display of the follower effect:

 Delphi |  copy code |? 
01
procedure TfrmMain.WMPaint(var Message: TWMPaint);
02
begin
03
  Inherited;
04
  Bmp.Canvas.Lock;
05
  try
06
    Canvas.Draw(0, 0, Bmp);
07
  finally
08
    Bmp.Canvas.Unlock;
09
  end;
10
end;

Take a look on the Canvas.Lock, that will create a basic switcher between modification and drawing processes.

Finally, to don’t raise a exception we need to terminate the parallel process before closing the form and on it destruction we have to free the Bmp to avoid memory leak.

 Delphi |  copy code |? 
1
procedure TfrmMain.FormDestroy(Sender: TObject);
2
begin
3
  Bmp.Free;
4
end;
5
 
6
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
7
begin
8
  TerminateAllParallelJobs;
9
end;

Full source without ParallelJobs library, Download it here: pj_example1 (1.27 KB) - 64 hits

Very nice and easy, uhm?

Hugs for all

Tags: , , ,


Leave a comment