• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Multi Threading With options.

hi, I need to create a thread with options like a function.

String1,String2
it will pass that though the thread and return the results

Thread('String1','String2');

Sal.
0
SaLz
Asked:
SaLz
  • 3
  • 3
1 Solution
 
vadim_tiCommented:
type

   TYourThread = class(TThread)
   private
      s1, s2: String;
      rs: String;
   protected
     procedure Execute; override;
  end;

procedure TYourThread.Execute;
begin
  rs := s1+s2;
end;

begin
  with TYourThread.Create(true) do begin
     s1 := 'String1';
     s2 := 'String2';
    Suspended := false;
   WaitFor;
   ShowMessage(rs);
  Destroy;
  end
end;
0
 
SaLzAuthor Commented:
hi, is there anyway of putting the results in another thread than in the same area that was called?
0
 
vadim_tiCommented:
i am not sure what you mean, may be something like this

type

   TYourThread = class(TThread)
   private
      s1, s2: String;
      rs: PChar;
   protected
     procedure Execute; override;
  end;

procedure TYourThread.Execute;
begin
  StrPCopy(rs, s1+s2);
end;



procedure SomeProcedure;
var
  forResult: array [0..200] of char;
begin
  with TYourThread.Create(true) do begin
     s1 := 'String1';
     s2 := 'String2';
     rs := forResult;
    Suspended := false;
   WaitFor;
   ShowMessage(forResult);
  Destroy;
  end
end
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SaLzAuthor Commented:
lol you just posted the same comment lol, what I ment was.


we have a Function like

Thread('String1',String2')

now it will execute the code..
TYourThread.Execute;

and then it will use another one to display the code
TYourThread.Display;

so we start with a button1click..

procedure TForm1.Button1Click(Sender: TObject);
begin
   Thread('String1',String2');
end;

it when then go to the execute..

procedure TYourThread.Execute;
begin
  rs := s1+s2;
end;

to display it
procedure TYourThread.Display;
begin
  form1.memo1.lines.add(rs);
end;


I have this thread but for some reason can't get it 2 work..

type
  TThread = class(TThread)
  private
    fUrl, fAction, fRow,fItem : string;
    procedure ShowPage;
  protected
  public
    property Url : string  read fUrl  write fUrl;
    property Item : string  read fItem  write fItem;
    property Action : string read fAction  write fAction;
    property Row : string read fRow  write fRow;
    procedure Execute; override;
    destructor Destroy; override;
  end;

procedure TThread.Execute;
begin
  inherited;
  rs:=Http.Get(Url);
  Synchronize(ShowPage);
end;

procedure TThread.ShowPage;
var
  Thread1,Thread2 : TThread;
begin
   form1.memo1.lines.add(rs);
end;




procedure TForm2.Button1Click(Sender: TObject);
var
  Thread1,Thread2 : TThread;
begin
  Thread1 := TThread.Create(True);
  Thread1.Url := 'http://www.google.com/;
  Thread1.Action:='add';
  Thread1.Resume;
end;


if u understand what I mean? Run>>>Execute>>>Display, it needs to be multi threaded, so lots can be done at the same time.
0
 
vadim_tiCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TMyThread = class(TThread)
  private
    HTTP: TIdHTTP;
    url, rs: String;
    procedure Display;
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;
  end;

constructor TMyThread.Create;
begin
  inherited Create(true);
  HTTP := TIdHTTP.Create(Application);
  HTTP.HandleRedirects := true;
  FreeOnTerminate := true;
end;

destructor TMyThread.Destroy;
begin
  HTTP.Free;
  inherited;
end;

procedure TMyThread.Display;
begin
  Form1.Memo1.Lines.Add(rs);
  Form1.Memo1.Lines.Add('***********************');
end;

procedure TMyThread.Execute;
begin
  try
    rs := Http.Get(Url);
  except
    on e:exception do
      rs := e.Message;
  end;
  Synchronize(Display);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  n: Integer;
begin
  for n:=1 to 20 do
    with TMyThread.Create do begin
      Url := 'http://www.google.com/';
      Resume;
    end;
end;

end.
0
 
SaLzAuthor Commented:
thxs :D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now