multithreading - Multithread interactions -


main form:

unit unit3;  interface  uses   winapi.windows, winapi.messages, system.sysutils, system.variants, system.classes, vcl.graphics,   vcl.controls, vcl.forms, vcl.dialogs, vcl.stdctrls;  type   tform3 = class(tform)     memo1: tmemo;     button1: tbutton;     button2: tbutton;     procedure button1click(sender: tobject);   private     { private declarations }   public     { public declarations }     class var counter : integer;   end;  var   form3: tform3;  implementation  {$r *.dfm}  uses unit4, unit5;  procedure tform3.button1click(sender: tobject); var   th1 : thcounter;   th2 : thprinter; begin   th1:= thcounter.create;   th2:= thprinter.create; end;  end. 

thread counter :

unit unit4;  interface  uses   system.classes, unit3;  type   thcounter = class(tthread)   private     { private declarations }   protected     procedure execute; override;   end;  implementation  { thcounter }  procedure thcounter.execute; var   i: integer;   printval : integer; begin   { place thread code here }   printval:= 50;   := 0 1000000000   begin     form3.counter:= i;     if form3.counter = printval     begin       // run print thread    ????       printval:= printval + 50;     end;   end; end;  end. 

thread print :

unit unit5;  interface  uses   system.classes, unit3;  type   thprinter = class(tthread)   private     { private declarations }     procedure printit;   protected     procedure execute; override;   end;  implementation  uses   system.sysutils;  { thprinter }  procedure thprinter.execute; begin   { place thread code here }   synchronize(printit); end;  procedure thprinter.printit; begin   form3.memo1.lines.add(inttostr(form3.counter)); end;  end. 

i workin on simple project. stuck.

i have 2 thread thcounter , thprint. thcounter, increase counter 1 billion. , want call thread ( thprint ) when counter 50 , multiples 100, 150, 200 print screen in tmemo....

how can send message thprint thcounter?

to signal other thread, use synchronization primitive, tsimpleevent.

let owned thcounter thread, , pass reference when thprinter created.

in thprinter.execute:

while not terminated begin   if waitevent.waitfor(100) = wrsignaled // keep listening terminated flag   begin     synchronize(printit);   end; end; 

and in thcounter:

waitevent.setevent;  // triggers printit  

just create waitevent automatically reset after event has been triggered.

waitevent := tsimpleevent.create(nil, false,false,''); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -