Link to home
Start Free TrialLog in
Avatar of campid
campid

asked on

API Threads

How can I get a function to run in its own thread using the api thread function 'CreateThread'?

Handle := CreateThread( nil, // no security                                  
                          0,   // the same stack size                          
                          @ThreadFunc, // thread entry point                    
                          @Self,      // parameter to pass to ThreadFunc        
                          CREATE_SUSPENDED,   // always SUSPENDED              
                          FThreadID ); // receive thread ID
ASKER CERTIFIED SOLUTION
Avatar of Lee_Nover
Lee_Nover

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it is better in delphi to use BeginThread instead of CreateThread, See the Delphi Help for some info about this. Here is some code I did for a timer thread with some comments.


function ThreadFunc(Parameter: Pointer): Integer; stdcall;
begin
Result := 0;
while thdTiming do
  begin
  {thdTiming cuts this thread off, it uses a while loop with a
  SleepEx to get a timer interval effect}
  SleepEx(TimerR.Interval, False);
  case TimerR.ID of
    1: if thdTiming then FlipIt;
    {check for thdTiming here also in case it changed
    during SleepEx}
    2: if thdTiming then MoveBall;
    else Break;
    end;
  end;
TimerR.ID := 0;
EndThread(Result);
{ExitThread(Result);}
{no code below EndThread will be executed}
TextOut(FormDC,4, 100, 'Below EndThread, will not show', 15);
end;

procedure TimerStart;
begin
ResumeThread(hThread);
end;

procedure DoThreadTimer(Interval, TimerID: Word);
var
ThreadId: LongWord;
begin
{The interval should be more than 4 or alot of the processor
time cycles are used, if you need an interval less than 5 then
you may want to set the Thread Priority higher with
SetThreadPriority( )}
if TimerR.ID <> 0 then Exit;
{TimerR.ID is set to 0 when the Thread ends, so check for 0
to make sure you don't start a thread if one is still running}
if Interval < 5 then
TimerR.Interval := 5 else
TimerR.Interval := Interval;
TimerR.ID := TimerID;
CloseHandle(hThread);
{since a thread may be created more than once,
you should Close the Handle}

{if you wanted to use more than one thread then you should keep track
of each thread's handle and close it}
hThread := BeginThread(nil, 0, @ThreadFunc, nil, CREATE_SUSPENDED, ThreadId);
{BeginThread( ) is a Delphi System function which does the same as
windows CreateThread( ), except it sets the global IsMultiThread
variable, thereby making the heap thread-safe. Below is the CreateThread( )
function, which will also work. You need to Start the thread with ResumeThread( )}

{hThread := CreateThread(
        NIL,                        // no security attributes
        0,                           // use default stack size
        @ThreadFunc,                  // thread function
        nil,                // argument to thread function
        0,                           // use default creation flags
        ThreadId);}
       
{SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);}

{use EndThread( ) with BeginThread( ) and use ExitThread( ) with
CreateThread( ), see ThreadFunc( ) above}
end;


- - - - - - - - - -
and here is what the Win32 API Help says about TerminateThread

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination.

ExitThread may be better for CreateThread, unless you get an out of control thread.
Avatar of Lee_Nover
Lee_Nover

he asked :
How can I get a function to run in its own thread using the api thread function 'CreateThread'?
so I replied to that question directly
and yes ExitThread is better ...
but in the case I wrote TerminateThread is also ok cause it's the end of a simple procedure
I actually wanted to add a second post to write about TerminateThread/ExitThread ... but It didn't feel necessary
listening,..
Oh, I guess I should have mentioned that it is best if in your App's OnClose or OnDestroy you can call
CloseHandle(hThread);
to clear the OS thread object.
can somebody please tell wtf is this 'listening' thing you all keep posting ???
it's really annoying
Lee_Nover, yea the "listening" or "interested" or any other single word comment places that expert on the get Email message for new postings to that question and free access to the question after an answer has been accepted
Avatar of campid

ASKER

Lee_Nover,

Just what I was looking for. There was me thinking it was going to be a complexed thing.

Thanks to the other guys for your detailed comments but Lee was the first on the seen.


Why were on the topic, is there anyway to create a thread and keep it running after your program terminates?

Cheers, Ian
slick tnx :)

campid: no, because when a process terminates it closes all it's spawned threads !

another thing .. you can pass parameters to your function
and this is the more correct implementation
the threadproc should be a function that accepts one pointer parameter and returns integer with stdcall calling convention
and the code :

var hThread: THandle;
   hThdID: Cardinal;

const
 sStr = 'This is running in its own thread'#13#10'launched from thread id: %d - process id: %d';

[code]
type
  PStuff = ^TStuff;
  TStuff = packed record
    p1, p2: Integer;
  end;

function ThdProc(param: pointer): Integer;stdcall;
var lpPar: PStuff;
begin
     Result:=0;
     lpPar:=PStuff(param);
     MessageBox(0, PChar(Format(sStr, [lpPar^.p1, lpPar^.p2])), 'some caption', mb_OK);
     Dispose(lpPar); // we need to free up the memory
     ExitThread(0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var lpPar: PStuff;
begin
     New(lpPar);
     lpPar^.p1:=GetCurrentThreadID;
     lpPar^.p2:=GetCurrentProcessId;
     hThread:=CreateThread(nil, 0, @ThdProc, lpPar, CREATE_SUSPENDED, hThdID);
     if hThread > 0 then
        ResumeThread(hThread);
end;
[/code]
you probally should not create a thread with CREATE_SUSPENDED if you do not need to sync it with another thread, I avoid using CREATE_SUSPENDED, if at all posible.
yes but you might want to set it's priority prior to runing it :)
or something else :)