Solved

API Threads

Posted on 2002-03-31
11
784 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:campid
11 Comments
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 50 total points
ID: 6909290
var hThread: THandle;
    hThdID: Cardinal;

procedure ThdProc;
begin
     MessageBox(0, 'This is running in its own thread', 'some caption', mb_OK);
     TerminateThread(hThread, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     hThread:=CreateThread(nil, 0, @ThdProc, nil, CREATE_SUSPENDED, hThdID);
     if hThread > 0 then
        ResumeThread(hThread);
end;
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6909751
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.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6910268
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
0
 
LVL 3

Expert Comment

by:vladh
ID: 6911294
listening,..
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6912075
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6912085
can somebody please tell wtf is this 'listening' thing you all keep posting ???
it's really annoying
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6912134
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
0
 

Author Comment

by:campid
ID: 6912359
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
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6912495
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]
0
 
LVL 33

Expert Comment

by:Slick812
ID: 6914674
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.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6915073
yes but you might want to set it's priority prior to runing it :)
or something else :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now