pass params to the thread procedure

like if i had:

procedure displaynumber(i:integer);
begin
messagebox(0,pchar(inttostr(i)),'test',MB_ICONSTOP);
end;

and i wanted to start 10 threads of that:

beginthread(0,nil,@displaynumber,nil,0,th1);


The code I have now is:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type TNumberParam=record
i:integer;
end;

var
Form1: TForm1;

implementation

procedure displaynumber(i:integer);
begin
messagebox(0,pchar(inttostr(i)),'test',MB_ICONSTOP);
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
th1:cardinal;
numberparam:TNumberparam;
begin
numberparam.i:=9;
beginthread(nil,0,@displaynumber,@numberparam,0,th1);
beginthread(nil,0,@displaynumber,@numberparam,0,th1);
beginthread(nil,0,@displaynumber,@numberparam,0,th1);

end;

end.



It should display 3 messageboxes simultaneoutly with number 9! but doesnt!
can someone please explain why?

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion