About TThread

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

lurcAsked:
Who is Participating?
 
ghelbereCommented:
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
 
simonetCommented:
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
 
MadshiCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
aacrgCommented:
Is ResolveIP a class function or a constructor?
0
 
ITugayCommented:
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
 
MadshiCommented:
Hey Igor, you got it! I wonder how I could miss that...   :-)
0
 
lurcAuthor Commented:
Yes, TWinshoe is a class, and ResoveIP is its function. What improper does this code has?

Regards,
Lurc
0
 
MadshiCommented:
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
 
ITugayCommented:
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
 
MadshiCommented:
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
 
ghelbereCommented:
ghelbere changed the proposed answer to a comment
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.