/* The original version of this program can be found at http://damb.dk */
#include <windows.h>
#include <stdio.h>

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
  int i = 0;
  DWORD MainThread = (DWORD )lpParameter;
  MSG msg;
  printf("Slave Thread Started\n");
  do
  {
    PostThreadMessage(MainThread, 1001, 0, 0);
    GetMessage(&msg, NULL, 0, 0xFFFF);
    printf("Slave Received: %d\n", msg.message);
  }
  while(i++ < 10);
  PostThreadMessage (MainThread, 1002, 0, 0);
  return 0;
}

int main(void)
{
  DWORD SlaveThreadId;
  DWORD MainThreadId = GetCurrentThreadId();
  MSG msg;

  HANDLE SlaveThread = CreateThread(NULL, 0, ThreadProc, (LPVOID )MainThreadId, 0,  &SlaveThreadId);

  if(!MainThreadId || !SlaveThreadId)
  {
    printf("Thread error!\n");
    return 0;
  }
  SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
  SetThreadPriority(SlaveThread, THREAD_PRIORITY_HIGHEST);

  do
  {
    GetMessage(&msg, NULL, 0, 0xFFFF);
    printf("Master Received: %d\n", msg.message);
    PostThreadMessage(SlaveThreadId, 1000, 0, 0);
  }
  while(msg.message != 1002);

  return 0;
}