Solved

TWinThread & TWinHTTP troubles

Posted on 2006-07-22
7
736 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

20 Experts available now in Live!

Get 1:1 Help Now