0

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.

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't you mean not modifying the same structures?
3

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.

1 Comment

+1 for "advanced topic". The OP might not even want parallel execution, yet that's what appears to be asked.
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.