Solved

TWinThread & TWinHTTP troubles

Posted on 2006-07-22
7
745 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
[X]
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
  • 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
Industry Leaders: 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!

 
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

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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