We help IT Professionals succeed at work.
Get Started

Newbie Thread Programming

Allan_Fernandes
on
576 Views
1 Endorsement
Last Modified: 2016-06-15
I am using Threads programming for the first time.
Can use the below mentioned Private variable 'sysAbort' as done below ?
Can I use sysNum Local Variable as done below ?
(I will be running only one instance of the thread at a time)


  private
    { Private declarations }
    sysAbort:Boolean ;

procedure TForm3.button_StartClick(Sender: TObject);
var
  sysNum:Integer ;
begin
  sysAbort:=False ;
  sysNum:=0 ;
  memo1.lines.add('Starting') ;
  with TThread.CreateAnonymousThread(
    procedure
    var
      MaxValue:Int64 ;
      I: Integer;
      Total: Integer;
    begin
      MaxValue:=1000 ;
      for I := 1 to MaxValue do
      begin
        if sysAbort then  // Can I use thus ?
          break ;
        sysNum:=I ;  // Can I use local variable
        // memo1.lines.Add('Violated  ' + Inttostr(sysNum)) ;  // This works too !
        if (I * 10 mod MaxValue) = 0 then
          TThread.Synchronize (TThread.CurrentThread,
            procedure ()
            begin
              memo1.lines.Add('Progress : ' + Inttostr(I)) ;
            end);

        sleep(10) ;   // just for delay
      end;

      TThread.Synchronize (TThread.CurrentThread,
        procedure ()
        begin
          memo1.lines.Add('End of process : ' + IntToStr(sysNum));
        end);
    end

  ) do
  begin
    OnTerminate := ThreadFinished ;
    Start;
  end;
end;

procedure TForm3.ThreadFinished(Sender: TObject);
begin
  if TThread(Sender).FatalException = nil then begin
    TThread.Queue(nil,
      procedure
      begin
        memo1.lines.Add('Via Queue after Finish');
      end
    );
  end;
end;

procedure TForm3.button_AbortClick(Sender: TObject);
begin
  sysAbort:=True ;
end;

Open in new window

Comment
Watch Question
Software architect
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE