Solved

TWinThread & TWinHTTP troubles

Posted on 2006-07-22
7
738 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short film showing how OnPage and Connectwise integration works.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now