?
Solved

TWinThread & TWinHTTP troubles

Posted on 2006-07-22
7
Medium Priority
?
749 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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