Solved

About TThread

Posted on 2000-05-10
11
387 Views
Last Modified: 2010-04-04
Dear all,

I want to put a segment of time-wasting code into TThead, but cannot handle it properly. Is there anyone out there can help me out.

[snippet]

type
        TIPThread = Class( TThread )
     private
        fsPrefix: String;
        fiStart, fiEnd: Integer;
        fGrid: TStringGrid;
    protected
        procedure Execute; override;
    published
        constructor Create( grid1: TStringGrid; sPrefix: String; iStart,
iEnd: Integer );
        destructor Destroy; override;
end;

constructor TIPThread.Create( grid1: TStringGrid; sPrefix: String; iStart,
iEnd: Integer );
begin
  inherited Create( True );
  Priority := TThreadPriority( tpNormal );
  FreeOnTerminate := True;
  Suspended := False;

  fsPrefix := sPrefix;
  fiStart := iStart;
  fiEnd := iEnd;
  fGrid := TStringGrid.Create( nil );
  fGrid := grid1;
end;

destructor TIPThread.Destroy;
begin
   fGrid.Free;
   inherited Destroy;
end;


procedure TIPThread.Execute;
var
    i, iRow : Integer;
    sHost, sTemp: String;
begin
   iRow := 1;                                   <= exception prompt from
here, everytime it
   for i := fiStart to fiEnd do           <= occurs on different line and
different contents
   begin                                           <= finally all dead, just
reboot PC.
      sHost := '';
      sTemp := fsPrefix + IntToStr( i );
      sHost := TWinshoe.ResolveIP( sTemp );
      if Length( sHost ) <> 0 then
      begin
            if iRow > 1 then
                fGrid.RowCount := fGrid.RowCount + 1;
            fGrid.Cells[0, iRow] := sHost;
            fGrid.Cells[1, iRow] := sTemp;
            Inc( iRow );
      end;
   end;
end;

procedure TfrmConfig.btnRetrieveClick(Sender: TObject);
var
     i, iStart, iEnd: Integer;
     sStart, sEnd, sTemp, sPrefix, sHost: String;
     IPThread: TIPThread;
begin
    .......
   IPThread := TIPThread.Create( grdHost, sPrefix, StrToInt( sStart ),
StrToInt( sEnd ) );
   try
      IPThread.Execute;
   finally
      IPThread.Free;
   end;
end;

The main purpose of  TIPThread is to detect local network IP address/host
name dynamically

fGrid is a TStringGrid control used to show search result
fsPrefix is first three section of IP address, 192.168.0
fiStart & fiEnd are search range, like 1, 10, that mean search 192.168.0.1
to 192.168.0.10.

Any help is appreciated.

--
Best regards,
Lurc

0
Comment
Question by:lurc
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 15

Expert Comment

by:simonet
ID: 2798543
You are using a thread to change data on a visual component. That can only be done using a Synchronize function (part of the Thread's class).

Yours,

Alex

BTW: You've asked the same Q twice. You may want to delete the other one, since it doesn't have any comments yet.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2799046
Alex is right,

but this looks strange to me, too:

sHost := TWinshoe.ResolveIP( sTemp );

Is TWinshoe a class? Then there's something wrong.

Regards, Madshi.
0
 

Expert Comment

by:aacrg
ID: 2799266
Is ResolveIP a class function or a constructor?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2799422
Hi all,
aacrq,

Someting wrong with this part:

 IPThread := TIPThread.Create(...);
 try
   IPThread.Execute;
 finally
   IPThread.Free;
 end;

1. You created thread is not suspended and youor thread Execute method start immediatly after create. You do not need to call it manually.

2. You call Free method, but in this moment your thread may be inside Execute. You trying to free thread while it in progress. Wait till it finished or Terminate it.

----
Igor
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2799426
Hey Igor, you got it! I wonder how I could miss that...   :-)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:lurc
ID: 2799458
Yes, TWinshoe is a class, and ResoveIP is its function. What improper does this code has?

Regards,
Lurc
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2799587
If it is a class function, everything is alright. But if it is a normal function, you first have to create an instance of that class. You always have to do in object oriented programming.

  with TWinshoe.Create(...) do
    try
      ResolveIP(...);
    finally Free end;

Regards, Madshi.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2799590
Lurc,
if TWinshoe is class and ResolveIP its function (instead constructor) then it's realy look strange. Usually it look like

....
var Winshoe : TWinshoe;
....
  Winshoe := TWinshoe.Create;
  Winshoe.ResolveIP(...)
....
  Winshoe.Free;

----
Igor

PS: Ddid you try to wait till you thread is fihishes? Is it solve you problem?
0
 

Accepted Solution

by:
ghelbere earned 200 total points
ID: 2801720
First, I must notice that you have some wrong code here ...
1. You should better resume thread after all initialization part is done.
(Suspended := False must be the last line in the constructor, anyway).
2. fGrid := Grid1 doesn't assign the address of Grid1 to fGrid. You seem to expect to have the results in the grdHost control from the frmConfig. Your results will be lost for sure.
You may use the grdHost object from frmConfig in a "Synchronized" section and everything will be OK with this error.
3. The real problems appears because your thread is started immediately after its creation, you try to relaunch the running thread procedure while it was still executing, you do not wait for thread to finish and you have terminated it brutally. Anyway, if you don't use WaitFor to wait the thread to finish, your thread will crash because you have declare it as a local procedure object. When the btnRetrieveClick procedure finishes, your thread will be "in the air".
If you really want more informations about thread management, you may write to me at ghelbere@yahoo.com and I'll do the best for you.

          Good luck,
          Stefan GHELBERE
          SPS Romania SRL
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2802232
And another new expert! Welcome, Stefan...   :-)

May I friendly request something? Here in the Delphi forum it is common practice to lock questions
with an answer only if you are the first expert in the thread, or if all other comments are evidently wrong. Otherwise it is
usual to post a *comment* only. This way the questioner can later easily choose which expert helped most.
 
Thank you for listening...   (-:

Regards, Madshi.
0
 

Expert Comment

by:ghelbere
ID: 2802360
ghelbere changed the proposed answer to a comment
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

19 Experts available now in Live!

Get 1:1 Help Now