views:

210

answers:

5

hi im new to delphi. how to use the two function at the same time? function a(...):integer; function b(...):integer;

because b waits until a is finshed.

+3  A: 

It is done using threads. It is an advanced topic, however, and you can better first learn the basics of programming before you start with threads.

Sjoerd
+1 for "advanced topic". The OP might not even want parallel execution, yet that's what appears to be asked.
Cosmin Prund
+4  A: 

you probobly need Multi-threading. you can start with tutorial in about.com

XBasic3000
thanx for the links... its a good jumpstart..
simply_anny
+1  A: 

there's also "AsyncCalls" library which does what you need. Please have a look at http://andy.jgknet.de/blog/?page_id=100

migajek
+7  A: 

Assuming Delphi 2009 or above and using OmniThreadLibrary:

uses OtlParallel;

var
  aRes: integer;
  bRes: integer;

begin
  Parallel.Join(
    procedure begin
      aRes := a();
    end,
    procedure begin
      bRes := b();
    end);
end.

Or for purists who don't like anonymous functions:

uses OtlParallel;

var
  aRes: integer;
  bRes: integer;

procedure CalcA;
begin
  aRes := a();
end;

procedure CalcB;
begin
  bRes := b();
end;

begin
  Parallel.Join(CalcA, CalcB);
end.

(It work work the same if CalcA and CalcB are methods, not plain procedures.)

As others have stated, the field of multithreading programming is full of danger. Make sure that your two functions are not modifying same structures, not outputing data to a same destination and, most of all, that they are not using GUI in any way.

gabr
Don't you mean **not** modifying the same structures?
afrazier
@afrazier: Of course! Thanks for the correction.
gabr
+1  A: 

can you explain in detail? because, maybe we can find another solution for you...

best regards,

Radu Barbu