Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TWinThread & TWinHTTP troubles

Posted on 2006-07-22
7
743 Views
Last Modified: 2012-06-27
Hello,
i'm having some problems with TWinThread component (appcontrols.com)
i dont understand why once the thread is created and executed by using the button3 (see sourcecode),
the WinHTTP component onDone Event (HTTPClient.OnDone) doesn't work properly..

eg. in the Memo1 output i get this:

---------------------
Done with 3 Kb 12/30/1899
Terminated

Terminated

Terminated
----------------------

as you can see the HTTPClient.OnDone event runs just once.. how do i fix this? i can't figure it out. please help
here below i posted the source code:





uses
  WinHTTP, WinThread;



  private
    nThreadCount_id: integer;
  public
    procedure Log(in_slog: string; in_nid: integer);




var
  WinThread: TWinThread;
  HTTPClient: TWinHTTP;



{------------------------------------------------------------------------------}
procedure TForm1.Get(in_sUrl: string; in_sPostData: string);
begin

Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; connecting...', WinThread.Tag);
HTTPClient := TwinHTTP.Create(nil);
HTTPClient.Agent        := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
HTTPClient.URL          := in_sURL;
HTTPClient.POSTData     := in_sPostData;
HTTPClient.Tag          := WinThread.Tag;
HTTPClient.OnDone       := pDone;
HTTPClient.Read(TRUE);

end;
{------------------------------------------------------------------------------}
procedure TForm1.pDone(Sender: TObject; const ContentType: String;
  FileSize: Integer; Stream: TStream);
var
  td : TDateTime;
begin
     Form1.Memo1.Lines.Add('Done with ' + IntToStr(FileSize div 1024) + ' Kb ' + DateTimeToStr(td));
end;
{------------------------------------------------------------------------------}
procedure TForm1.pExecute;
begin
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; started.', WinThread.Tag);
   Form1.ListBox1.Update;
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; connecting...', WinThread.Tag);
   Form1.Get('www.google.com', '');
end;
{------------------------------------------------------------------------------}
procedure TForm1.pTerminate;
begin
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; Terminated', WinThread.Tag);
   Form1.Memo1.Lines.Add('Terminated' + #13#10);
end;
{------------------------------------------------------------------------------}






procedure TForm1.Log(in_slog: string; in_nid: integer);
var
  DateTime: TDateTime;
begin
  DateTime := now;
  with ListBox1 do
  begin
    if Items.Count >= in_nid then
    begin
      Items.Strings[in_nid] := format('%s %s', [TimeToStr(DateTime), in_slog]);
    end
    else
      Items.Add(format('%s %s', [TimeToStr(DateTime), in_slog]));
  end;
end;

{------------------------------------------------------------------------------}

procedure TForm1.Button3Click(Sender: TObject);
var i: integer;
begin
  WinThread := TWinThread.Create(nil);
  WinThread.OnExecute   := pExecute;
  WinThread.OnTerminate := pTerminate;
  WinThread.Execute;
end;




btw: if you need these components, i will send you a copy.
0
Comment
Question by:xapsx
  • 5
  • 2
7 Comments
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 17162261
what do you mean it "doesn't work properly.."?

It does not fire? It fires for me.
0
 

Author Comment

by:xapsx
ID: 17162290
yes it doesn't fire..
can you post the full source here?

thanks
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 17162364
uses
  WinThread, WinHTTP;

type
  TForm1 = class(TForm)

    btn1: TButton;
    mmo1: TMemo;
    lst1: TListBox;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    nThreadCount_id: integer;
  public
    { Public declarations }
    procedure Log(in_slog: string; in_nid: integer);
    procedure Get(in_sUrl: string; in_sPostData: string);
    procedure pDone(Sender: TObject; const ContentType: String;
                    FileSize: Integer; Stream: TStream);
    procedure pExecute(Sender: TObject);
    procedure pTerminate(Sender: TObject);
  end;

var
  Form1: TForm1;
    HTTPClient: TWinHTTP;
    WinThread: TWinThread;

implementation

{$R *.dfm}

{------------------------------------------------------------------------------}
procedure TForm1.Get(in_sUrl: string; in_sPostData: string);
begin

  Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; connecting...', WinThread.Tag);
  HTTPClient := TWinHTTP.Create(nil);
  HTTPClient.Agent        := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
  HTTPClient.URL          := in_sURL;
  HTTPClient.POSTData     := in_sPostData;
  HTTPClient.Tag          := WinThread.Tag;
  HTTPClient.OnDone       := pDone;
  HTTPClient.Read(TRUE);

end;
{------------------------------------------------------------------------------}
procedure TForm1.pDone(Sender: TObject; const ContentType: String;
  FileSize: Integer; Stream: TStream);
var
  td : TDateTime;
begin
     Form1.mmo1.Lines.Add('Done with ' + IntToStr(FileSize div 1024) + ' Kb ' + DateTimeToStr(td));
end;
{------------------------------------------------------------------------------}
procedure TForm1.pExecute(Sender: TObject);
begin
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; started.', WinThread.Tag);
   Form1.lst1.Update;
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; connecting...', WinThread.Tag);
   Form1.Get('www.google.com', '');
end;
{------------------------------------------------------------------------------}
procedure TForm1.pTerminate(Sender: TObject);
begin
   Form1.Log('Thread id: ' + IntToStr(WinThread.Tag+1) + '; Terminated', WinThread.Tag);
   Form1.Mmo1.Lines.Add('Terminated' + #13#10);
end;
{------------------------------------------------------------------------------}

procedure TForm1.Log(in_slog: string; in_nid: integer);
var
  DateTime: TDateTime;
begin
  DateTime := now;
  with lst1 do
  begin
    if Items.Count >= in_nid then
    begin
      Items.Strings[in_nid] := format('%s %s', [TimeToStr(DateTime), in_slog]);
    end
    else
      Items.Add(format('%s %s', [TimeToStr(DateTime), in_slog]));
  end;
end;

{------------------------------------------------------------------------------}

procedure TForm1.btn1Click(Sender: TObject);
var i: integer;
begin
  WinThread := TWinThread.Create(nil);
  WinThread.OnExecute   := pExecute;
  WinThread.OnTerminate := pTerminate;
  WinThread.Execute;
end;

end.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:LMuadDIb
ID: 17162366
Im using delphi7 and the latest TWinHTTP by AppControls (demo version)
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 17162384
0
 
LVL 4

Accepted Solution

by:
LMuadDIb earned 125 total points
ID: 17162389
sample output from the code I posted:

------

Done with 3 Kb 12/30/1899
Done with 3 Kb 12/30/1899
Terminated

Terminated

Done with 3 Kb 12/30/1899
Terminated

Done with 3 Kb 12/30/1899
Terminated

Done with 3 Kb 12/30/1899
Terminated
0
 

Author Comment

by:xapsx
ID: 17163345
i started a new project using your code, and it worked.  seems like it was a settings problem. thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 94
Convert Jpg /PNG To GIF 5 150
Internet Explorer View Settings Question 15 116
Delphi selector screen 2 84
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question