Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Keeping Thread Active till termination of Program

I'm trying to keep my secondary thread continously running untill my program terminates. My secondary thread is created upon the create function of my main form. I am destroying my thread when i exit my main form. I tried putting
while not secondthread.terminated do
secondthread.waitfor

then i terminate the thread on exit of the mainform. Any suggestions on how to get this accomplished? thanks

Comments

  • zibadianzibadian Member Posts: 6,349
    [b][red]This message was edited by zibadian at 2002-10-14 13:14:44[/red][/b][hr]
    : I'm trying to keep my secondary thread continously running untill my program terminates. My secondary thread is created upon the create function of my main form. I am destroying my thread when i exit my main form. I tried putting
    : while not secondthread.terminated do
    : secondthread.waitfor
    :
    : then i terminate the thread on exit of the mainform. Any suggestions on how to get this accomplished? thanks
    :
    Just create a new TThread descendant, then don't call the Terminate method anywhere except in the Form1.OnDestroy event. Here is an small example:
    [code]
    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    Menus;

    type
    TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private
    FSecondThread: TThread;
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.DFM}

    type
    { Declare new type of thread to override the execute }
    TSomeThread = class(TThread)
    protected
    procedure Execute; override;
    public
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    FSecondThread := TSomeThread.Create(false);
    // Create and start thread
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    FSecondThread.Terminate;
    // Terminate and free thread
    if FSecondThread <> nil then
    FSecondThread.WaitFor;
    // Wait for Thread to terminate
    end;

    { TSomeThread }

    procedure TSomeThread.Execute;
    begin
    FreeOnTerminate := true;
    while not Terminated do begin
    { Do something useful }
    end;
    end;

    end.
    [/code]


  • uchetfielduchetfield Member Posts: 10
    Thanks, i didn't have the part of the code to waitfor if the my secondary thread<> nil.


    : [b][red]This message was edited by zibadian at 2002-10-14 13:14:44[/red][/b][hr]
    : : I'm trying to keep my secondary thread continously running untill my program terminates. My secondary thread is created upon the create function of my main form. I am destroying my thread when i exit my main form. I tried putting
    : : while not secondthread.terminated do
    : : secondthread.waitfor
    : :
    : : then i terminate the thread on exit of the mainform. Any suggestions on how to get this accomplished? thanks
    : :
    : Just create a new TThread descendant, then don't call the Terminate method anywhere except in the Form1.OnDestroy event. Here is an small example:
    : [code]
    : unit Unit1;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    : Menus;
    :
    : type
    : TForm1 = class(TForm)
    : procedure FormCreate(Sender: TObject);
    : procedure FormDestroy(Sender: TObject);
    : private
    : FSecondThread: TThread;
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : {$R *.DFM}
    :
    : type
    : { Declare new type of thread to override the execute }
    : TSomeThread = class(TThread)
    : protected
    : procedure Execute; override;
    : public
    : end;
    :
    : procedure TForm1.FormCreate(Sender: TObject);
    : begin
    : FSecondThread := TSomeThread.Create(false);
    : // Create and start thread
    : end;
    :
    : procedure TForm1.FormDestroy(Sender: TObject);
    : begin
    : FSecondThread.Terminate;
    : // Terminate and free thread
    : if FSecondThread <> nil then
    : FSecondThread.WaitFor;
    : // Wait for Thread to terminate
    : end;
    :
    : { TSomeThread }
    :
    : procedure TSomeThread.Execute;
    : begin
    : FreeOnTerminate := true;
    : while not Terminated do begin
    : { Do something useful }
    : end;
    : end;
    :
    : end.
    : [/code]
    :
    :
    :

  • Shawn CarterShawn Carter Member Posts: 0

    _____ [ http://forcoder.org ] free ebooks and video tutorials about ( Visual Basic Scratch Assembly Delphi R JavaScript C++ C C# PHP Go PL/SQL Objective-C MATLAB Java Ruby Perl Python Visual Basic .NET Swift F# Lua ABAP Kotlin Erlang FoxPro Prolog Dart Rust ML D VBScript Logo Transact-SQL Lisp Fortran Crystal Hack Julia Apex Clojure Scheme Alice Scala Ada Awk COBOL SAS Bash LabVIEW ) __________

Sign In or Register to comment.