Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Thread Problem, Please Assist !!! 1

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hi all, I have made a single thread, to generate random
words for me ... But I have a problem with the thread.
When I tell it to terminate it keeps generating words
for me.

Even though Delphi says that my thread is terminated, it
just keeps going.

Here is my code:
Code:
type
  TGeneratorThread = class(TThread)
  protected
    Letters: array[0..25] of PChar;
    procedure FillArray;
    procedure Execute; override;
  end;

... bla bla bla ...

procedure TGeneratorThread.FillArray;
begin
    Letters[0]  := 'A';
    Letters[1]  := 'B';
    Letters[2]  := 'C';
    Letters[3]  := 'D';
    Letters[4]  := 'E';
    Letters[5]  := 'F';
    Letters[6]  := 'G';
    Letters[7]  := 'H';
    Letters[8]  := 'I';
    Letters[9]  := 'J';
    Letters[10] := 'K';
    Letters[11] := 'L';
    Letters[12] := 'M';
    Letters[13] := 'N';
    Letters[14] := 'O';
    Letters[15] := 'P';
    Letters[16] := 'Q';
    Letters[17] := 'R';
    Letters[18] := 'S';
    Letters[19] := 'T';
    Letters[20] := 'U';
    Letters[21] := 'V';
    Letters[22] := 'W';
    Letters[23] := 'X';
    Letters[24] := 'Y';
    Letters[25] := 'Z';
end;

procedure TGeneratorThread.Execute;
var i, i2, LettersInWord, NumOfWords: Integer;
MyWord, MyPrevWord: String;
begin
Synchronize(FillArray);
Form1.StatusBar1.Panels[0].Text := '';
Form1.StatusBar1.Panels[1].Text := '';
Form1.StatusBar1.Panels[2].Text := '';
Form1.StatusBar1.Panels[3].Text := '';

i := 0;
LettersInWord := StrToInt(Form1.Edit2.Text);
NumOfWords := StrToInt(Form1.Edit1.Text);
Form1.ProgressBar1.Max := NumOfWords;

if Form1.CheckBox2.Checked then
Form1.RichEdit1.Clear;
Form1.RichEdit1.Refresh;

MyPrevWord := '';

if MessageDlg('Start generating the words ???',mtConfirmation,[mbNo,mbYes],0) = mrYes then
while i < NumOfWords do
  begin
    i2 := 0;
    Randomize;

    if not Form1.CheckBox1.Checked then
      MyWord := '';

    while i2 < LettersInWord do
      begin
      i2 := i2 + 1;
      MyWord := MyWord + String(Letters[Random(25)]);
      end;

    if MyPrevWord <> MyWord then
      begin
      Form1.RichEdit1.Lines.Add(MyWord);
      i := i + 1;
      Form1.ProgressBar1.Position := i;
      MyPrevWord := MyWord;
      Form1.StatusBar1.Panels[0].Text := IntToStr(i) + ' of the ordered ' + IntToStr(NumOfWords) + ' words';
      Form1.StatusBar1.Panels[1].Text := 'Generating ' + IntToStr(LettersInWord) + ' letter words';
      Form1.StatusBar1.Refresh;
      end;
  end;
Form1.ProgressBar1.Position := 0;
end;

I hope you guys can tell me why nothing happens when I do
this:

Code:
procedure button1click(sender: tobject);
var GenThr: TGeneratorThread;
begin
GenThr.Terminate;
end;

Thanks in advance for your help,
BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Sorry, I forgot to mention this:

I thought it might have something to do with the
Code:
while..do
loops in there ...
If so, can you guys show me how to get around that ???

Thank you, BobbaFet

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
It is.
When you call Terminate all you're doing is setting the Terminated property of the Thread.
Change
Code:
while i < NumOfWords do
to
Code:
while (i < NumOfWords) and (not Terminated) do
 
Also, why bother with that Letters[] array?

Just do this instead:
Code:
MyWord := MyWord + Chr(Ord('A') + Random(25));
 
what does that code do ???

Does it give me a random letter like it does now,
I dont want anything thats not in that array.

But thanx for your help with the terminating issue !!!
I really appreciate it !!! [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Yes, that code is equivalent exactly to yours.
It just adds a random value in the range 0-25 to the Ascii value of 'A', so you get characters in the range 'A'-'Z'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top