21
Jul 08
::
articles :: by Gilberto Saraiva
Camaradas,
Adicionei hoje o ParallelJobs no code.google,
Farei dele um espelho(mirror) do svn principal que é o:
http://devpartners.ath.cx:11520/svn/DevPartners/ParallelJobs/
E ainda criarei pra cada commit da library um snapshot, assim todos poderão ter o ParallelJobs, sem depender de um único servidor.
Mas e então, o que é o ParallelJobs?
O ParallelJobs é uma biblioteca que disponibiliza uma estrutura fácil para se criar processos paralelos em seu aplicativo Delphi. A forma mais conhecida de se fazer isso é utilizando extendendo a classe TThread, o que ainda torna o aplicativo dependente do gerênciamento principal de processos que a VCL dispoe. Com o ParallelJobs você podera criar os mesmos processos com apenas 1 linha de código além de poder fazer isso em qualquer lugar que precise, jogos, aplicativos matemáticos e vários outros modelos de aplicativos que não se utilize necessariamente a VCL.
Você podera acompanhar a evolução e alguns exemplos de utilização do ParallelJobs aqui, nos artigos em inglês.
Então vamos aos links do projeto:
Fórum:
http://devpartners.projects.pro.br/forum/?board=8
SVN principal:
http://devpartners.ath.cx:11520/svn/DevPartners/ParallelJobs/
TRAC:
http://devpartners.ath.cx:11520/trac/DevPartners/ParallelJobs/
SVN mirror:
http://paralleljobs.googlecode.com/svn/trunk/
Google code area:
http://code.google.com/p/paralleljobs/
Baixe um exemplo de utilização do ParallelJobs:
Download ele aqui:
Abraços a todos
articles — 1 comment
19
Jul 08
::
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:
| 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:
| 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:
| 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.
| 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) - 73 hits
Very nice and easy, uhm?
Hugs for all
articles — No comments
05
Jul 08
::
news :: by Gilberto Saraiva
Camaradas,
Ontem iniciei o ParallelJobs no DevPartners.
Mas o que é o ParallelJobs?
O ParallelJobs vai ser uma library para Delphi que consiste em disponibilizar facilitadores para criação de processos paralelos nas aplicações diversas.
A library ainda está no começo, mas já tem um aplicativo teste que vai servir para exemplificar algumas maneiras de utilização e criação de processos parallelos.
Acompanhe o fórum para maiores informações:
http://devpartners.projects.pro.br/forum/?board=8.0
Vou manter ele em inglês, mas isso não impede a comunicação em português de quem quiser contribuir.
façam bom uso e colaborem se puderem,
Abraços.
news — No comments