  '==========================
  'Window running in a thread
  '==========================


  uses MinWin
  uses GraphWin1A

  sys SharedData[256]

  '---------------------------------------
  function window(sys*dat) as sys external
  '=======================================
  dat[10]=123
  finit
  zstring* cmdline
  &cmdline=GetCommandLine
  sys instance=GetModuleHandle 0
  '
  WinMain instance,0,cmdline,SW_NORMAL
  '
  end function


  #define FALSE    0
  #define TRUE     1
  #define IGNORE   0           //Ignore signal
  #define INFINITE 0xFFFFFFFF  //Infinite timeout

  #define __in
  #define __out
  #define __in_opt
  #define __out_opt
  #define __in_ecount(nCount)

  #define MAX_THREADS 3

  'Sample custom data structure for threads to use.
  'This is passed by void pointer so it can be any data type
  'that can be passed using a single void pointer (LPVOID).
  '
  type dtype sys val1,val2

  typedef sys   HANDLE,SIZE_T
  typedef void  *LPVOID, *LPSECURITY_ATTRIBUTES, *LPTHREAD_START_ROUTINE
  typedef dword *LPDWORD

  '================================
  extern stdcall lib "kernel32.dll"
  '================================


  sys CreateThread
  (
    __in_opt  sys    lpThreadAttributes,
    __in      dword  dwStackSize,
    __in      sys    lpStartAddress,
    __in_opt  sys    lpParameter,
    __in      dword  dwCreationFlags,
    __out_opt dword* lpThreadId
  )


  DWORD WaitForMultipleObjects
  (
    __in DWORD nCount,
    __in_ecount(nCount) CONST HANDLE *lpHandles,
    __in BOOL bWaitAll,
    __in DWORD dwMilliseconds
  )



  BOOL CloseHandle
  (
    __in HANDLE hObject
  )


  end extern

  sys counter

  #define MAX_THREADS 1

  sys FunArray[MAX_THREADS]={@Window}
  sys SharedData[256]

  dword  dwThreadIdArray[MAX_THREADS]
  HANDLE hThreadArray[MAX_THREADS]
  int    i

  'Create MAX_THREADS worker threads.
  '----------------------------------

  for i=1 to MAX_THREADS
    '
    'Create the thread to begin execution on its own.
    '------------------------------------------------
    '
    hThreadArray[i] =
    CreateThread
    ( 
    null,              'default security attributes
    0,                 'use default stack size  
    FunArray[i],       'thread functions
    @SharedData,       'argument to thread function 
    0,                 'use default creation flags 
    dwThreadIdArray[i] 'returns the thread identifier
    )
    '
    print "ThreadID: " dwThreadIdArray[i]
    '
    'Check the return value for success.
    'If CreateThread fails, terminate execution. 
    'This will automatically clean up threads and memory. 
    '
  next 'End of main thread creation loop.
  '
  'print "All threads up and running"
  '


  '--------------------
  sub finish() external
  '====================
  '
  'Wait until all threads have terminated.
  '---------------------------------------
  '
  WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE)
  '
  'print SharedData[10] 'test
  '
  'Close all thread handles and free memory allocations.
  '-----------------------------------------------------
  '
  for i=1 to MAX_THREADS
    CloseHandle( hThreadArray[i] )
  next
  '
  'print "All thread are closed. Program is ready to terminate"
  '

  'RELEASE SYSTEM RESOURCES
  '------------------------
  '
  terminate
  '
  end sub


  finish() 'normally invoked by a host





