// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <time.h>
#include <stdlib.h>
#include <string.h>

#include <windows.h>

CRITICAL_SECTION cs;
HANDLE console;

void gotoxy(int x,int y)
{
   COORD pos;
   pos.X = x;
   pos.Y = y;
   SetConsoleCursorPosition(console, pos);
}

void loop1()
{
   char c[] = { '|', '/', '-', '\\', '|', '/', '-', '\\' };
   int i = 0;
   while(true)
   {
      EnterCriticalSection(&cs);  
      gotoxy(40,10);
      DWORD dummy;
      WriteConsole(console, &c[i % 8], 1, &dummy, NULL);
      LeaveCriticalSection(&cs);   
      Sleep(1000);
      i++;
   }
}

void loop2()
{
   while(true)
   {
      EnterCriticalSection(&cs);   
      gotoxy(30,1);
      time_t t = time(NULL);
      DWORD dummy;
      char *s = ctime(&t);
      WriteConsole(console, s, strlen(s), &dummy, NULL);
      LeaveCriticalSection(&cs);   
      Sleep(1000);
   }
}

DWORD __stdcall loop1dispatch(void *p)
{
   loop1();
   return 0;
}

DWORD __stdcall loop2dispatch(void *p)
{
   loop2();
   return 0;
}

void threadloop1()
{
   DWORD id;
   CreateThread(NULL,0,loop1dispatch,NULL,0,&id);
}

void threadloop2()
{
   DWORD id;
   CreateThread(NULL,0,loop2dispatch,NULL,0,&id);
}

int main()
{
   system("CLS");
   console = GetStdHandle(STD_OUTPUT_HANDLE);
   InitializeCriticalSection(&cs);   
   threadloop2();
   loop1();
   return 0;
}
