### Starting a Thread

To start a thread within a process, use either INonGUIThread or IThread to represent the thread. Pass the code you want the thread to run to either the thread's constructor or to its start() method. You can wrap your thread code as a global function, as a class method, or as an object reference.

These examples pass a user-defined function, threadedFunction(), to a thread to run when it calls start(). Functions passed to thread constructors must be declared to be of type _Optlink or type _System (_Optlink and _System functions return void).

> > void _Optlink myThreadedFunction()
> > {
> > // Your code, to be executed in the new thread, goes here.
> > }

If you want to thread a function that has arguments:

- _Optlink functions take a void* argument type.
- _System functions take an unsigned long argument type.

> > void* threadArgs;

The following examples illustrate how to launch a thread to run your user-defined method:

- Pass the code you want threaded to the constructor for either IThread or INonGUIThread; the new thread waits for the object's call to start() to begin execution.

> void main(){
> INonGUIThread myThread(myThreadedFunction, threadArgs);
> ...
> myThread.start();
> }

- Call the default constructor for either IThread or INonGUIThread; pass the code you want threaded to start().

> > void main(){
> > IIThread *myThread = new IIThread();
> > ...
> > myThread->start(myThreadedFunction, threadArgs);
> > }

- Pass a child of IThreadFn, in which you've implemented run(), to either an IThread or INonGUIThread constructor.

  > // Assume myThreadFn is a subclass of IThreadFn
  > IThread myThread(myThreadFn,threadArgs);
  > ...
  > myThread.start();
  > // myThread.start() automatically executes myThreadFn::run()

- Pass a child of IThreadFn, in which you've implemented run(), to either an IThread's or INonGUIThread's start().

  > IThread myThread();
  > ...
  > // Assume myThreadFn is a subclass of IThreadFn
  > myThread.start(myThreadFn,threadArgs);
  > // myThread.start() automatically executes myThreadFn::run()