Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:10:44+00:00 2026-05-29T07:10:44+00:00

So my code does this: Download page in thread Parse the page Send it

  • 0

So my code does this:

  1. Download page in thread
  2. Parse the page
  3. Send it to main thread

All that is done over critical section and postmessage .
IF anyone could review it fix it change it , or anything else that could make it better.

Main VCL form :

    unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OverbyteIcsWndControl, OverbyteIcsHttpProt, StdCtrls,Unit2;
const
  WM_DATA_IN_BUF = WM_APP + 1000;

type
  TForm1 = class(TForm)
    HttpCli1: THttpCli;
    Button1: TButton;
    ListBox1: TListBox;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FStringSectInit: boolean;
    FGoogle: TGoogle;
    FStringBuf: TStringList;
    procedure HandleNewData(var Message: TMessage); message WM_DATA_IN_BUF;
  public
     StringSection: TRTLCriticalSection;
    property StringBuf: TStringList read FStringBuf write FStringBuf;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not FStringSectInit then
  begin
    InitializeCriticalSection(StringSection);
    FStringBuf := TStringList.Create;
    FStringSectInit := true;
    FGoogle := TGoogle.Create(true);
    SetThreadPriority(FGoogle.Handle, THREAD_PRIORITY_BELOW_NORMAL);
    try
      FGoogle.StartNum := 8;
    except
      on EConvertError do FGoogle.StartNum := 2;
    end;
    FGoogle.Resume;
  end;
end;

procedure TForm1.HandleNewData(var Message: TMessage);
var i:integer;
begin
  if FStringSectInit then
  if listbox1.Items.Count<10 then
  begin
    EnterCriticalSection(StringSection);
    for i:=0 to 5 do
    if length(fstringbuf.Text)>10 then
    begin
    listbox1.Items.Add(FStringBuf.Strings[i]);
    end
    else
    FStringBuf.Clear;
    LeaveCriticalSection(StringSection);
    {Now trim the Result Memo.}
  end
  else
  begin
    with FGoogle do
    begin
      Terminate;
      WaitFor;
      Free;
    end;
   FGoogle := nil;
    FStringBuf.Free;
    FStringBuf := nil;
    DeleteCriticalSection(StringSection);
    FStringSectInit := false;
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if listbox1.Items.Count>80 then
end;

end.

And the Thread :

unit Unit2;

interface

uses
  Classes,Windows,IDHTTP, OverbyteIcsWndControl, StdCtrls,OverbyteIcsHttpProt,SysUtils,Dialogs;

type
  TGoogle = class(TThread)
  private
google:TStringList;
    Upit:string;
    Broj:integer;
    Buffer : TStringList;
  protected
    procedure parsegoogleapi;
    procedure SkiniSors;
    procedure Execute; override;
  public
    property StartNum: integer read Broj write Broj;
  end;

implementation
uses unit1,StrUtils;

function ExtractText(const Str, Delim1, Delim2: string; PosStart: integer; var PosEnd: integer): string;
var
 pos1, pos2: integer;
begin
    Result := '';
    pos1 := PosEx(Delim1, Str, PosStart);
    if pos1 > 0 then
    begin
    pos2 := PosEx(Delim2, Str, pos1 + Length(Delim1));
    if pos2 > 0 then
    begin
      PosEnd := pos2 + Length(Delim2);
      Result := Copy(Str, pos1 + Length(Delim1), pos2 - (pos1 + Length(Delim1)));
end;
end;
end;
function ChangeString(const Value: string; replace:string): string;
var i: Integer;
begin
    Result := '';
    for i := 1 to Length(Value) do
    if Value[i] = ' ' then
    Result := Result + replace
    else
    Result := Result + Value[i]
end;

(*Ovo je procedura za skidanje sorsa*)

procedure TGoogle.SkiniSors;
var
HttpCli1 : THttpCli;
criter:string;

begin

HttpCli1:=THttpCli.Create(nil);
google:=TStringList.Create;
criter:= ChangeString(Upit,'%20');

With HttpCli1 do begin
    URL            := 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=' + inttostr(broj) + '&rsz=large&q=index.php';
    RequestVer     := '1.1';
    RcvdStream := TMemoryStream.Create;
    try
        Get;
    except
        ShowMessage('There has been an error , check your internet connection !');
        RcvdStream.Free;
        Exit;
    end;

RcvdStream.Seek(0,0);
google.LoadFromStream(RcvdStream);
RcvdStream.Free;
 broj:=broj+8;
ParseGoogleApi;
end;
end;

procedure TGoogle.ParseGoogleApi;
var Pos: integer;
    sText: string;
begin
Buffer:= TStringList.Create;
  sText := ExtractText(google.Text, '"url":"', '","visibleUrl"', 1, Pos);
  while sText <> '' do
  begin
    buffer.Add(sText);

    sText := ExtractText(google.Text, '"url":"', '","visibleUrl"', Pos, Pos);
  end;
end;


procedure TGoogle.Execute;
var
  CurrentNum: integer;
  i:integer;

begin

  CurrentNum := Broj;
  while not terminated do
  begin
  skinisors;
      EnterCriticalSection(Form1.StringSection);
      for i:=0 to 5 do                        begin
      Form1.StringBuf.Add(buffer.strings[i]);
      end;
      LeaveCriticalSection(Form1.StringSection);
      PostMessage(Form1.Handle, WM_DATA_IN_BUF, 0, 0);

    end;


end;
end.

So I want to know how can I stop the thread when page timeouts or if user gets disconnected etc.. so that thread can be terminated properly.

I want to start 3 instances of this thread and every instance should access critical section take one link ex link[i] where i is integer value that is incremented by thread which has ownership over critical section at that moment. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:10:45+00:00Added an answer on May 29, 2026 at 7:10 am

    Give your thread a constructor, and in that constructor set FreeOnTerminate := True;.

    In the thread’s execute method you can then call Terminate; on page timeout or disconnection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a .f file there is code that does this: real Bob, avar ...
If there's any open source code that does this already I'm interested in hearing
My expect code does this: It does a ssh connect to another machine, sends
The code currently does this and the fgetpos does handle files larger than 4GB
Why does this code fail to display the category name Apples using the current
Why does this code: class A { public: explicit A(int x) {} }; class
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
Why does this code not print an exception stack trace? public class Playground {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.