Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Threads and application termination

Posted on 2006-06-01
8
Medium Priority
?
133 Views
Last Modified: 2010-08-05
Hi

I have an app in which a thread is called every 5mins:

  TFTPTh = class(TThread)
  private
  protected
    procedure Execute; override;
  end;

  WatekFTP : TFTPTh;

and somewhere in the proggy there is

          WatekFTP := TFTPTh.Create(False);
            repeat
              sleep(50);
              Application.ProcessMessages;
            until WatekFTP.Terminated;
          WatekFTP.Free;

this works fine.

but if i do an application.terminate while the thread is running i get a runtime error

tried to check if the thread exists and if it did it was ok but if it didn't then the runtime error came up

what is the best way to control threads to get rid of this problem ??
0
Comment
Question by:tadzio_blah
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 16809874
First off, you wouldnt need the Application.ProcessMessages in your repeat, sense it in the thread
Second if your going to "free" the object like that surround it with a try finally block

          WatekFTP := TFTPTh.Create(False);
          try
            repeat
              sleep(50);
              Application.ProcessMessages;
            until WatekFTP.Terminated;
          finally
            WatekFTP.Free;
          end;

how are you terminating the thread?

0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 16809949
ahh, I see...
your timer is for checking for when the thread is complete, and not in the thread itself.... sorry

set:
FreeOnTerminate := True;
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 16810177
but you must also make sure your not in a infinite loop in your thread
make sure its getting terminated, enclose your thread process in a try finally blocks

sleep(50) is pretty fast, if your gonna use that sleep/processmessages then you might want to use something slower
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Accepted Solution

by:
TheRealLoki earned 540 total points
ID: 16812123
If your thread is expected to end naturally in a short period of time, just do

WatekFTP.WaitFor;


If you want to kill the thread, then close once you are sure the thread has died, do

WatekFTP.Terminate;
WatekFTP.WaitFor;

as mentioned above, it might be a good idea to do have
FreeOnTerminate := True; in teh thread's create code.
If you do not want to mess with the thread create code, you can do this :-

WatekFTP := TFTPTh.Create(True); // start paused
WatekFTP.FreeOnTerminate := True;
WatekFTP.Resume; // start thee thread
0
 

Author Comment

by:tadzio_blah
ID: 16822556
the thread is used to upload some data to my ftp
i've allready tried all of the above and the runtime error still comes up :/

the thing is if a user clicks on close app (executes the application.terminate in the main thread) while the WatekFTP thread is running then the user gets the error

and if i put:
if not(WatekFTP.Terminated) then
repeat
sleep(500);
until WatekFTP.Terminated;

in the closing procedure and the thread is allready freed the error comes up (but if i won't do the WatekFTP.Free then it's ok but in a short time the app will use a great deal of memory)
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16837316
ok, what is your thread doing in the main vcl?
does it, for instance, update a progress bar?
It sounds to me, like your form has begun to close, and the thread is trying to do something with the form, and getting an access violation.
are you doing "synchronize()" for every call to the main vcl the thread does?
are you checking "if terminated" in the thread?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

580 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