/* 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) */
#pragma once

namespace GUI
{
   using namespace System;
   using namespace System::ComponentModel;
   using namespace System::Windows::Forms;
   using namespace System::Drawing;
   using namespace System::Threading;
   public ref class Form1 : public Form
   {
   private:
      Label^ lbl;
      bool done;
      System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
      void InitializeComponent(void)
      {
         this->lbl = gcnew Label();
         this->SuspendLayout();
         this->lbl->Location = Point(75, 125);
         this->lbl->Name = L"lbl";
         this->lbl->Size = System::Drawing::Size(200, 25);
         this->ClientSize = System::Drawing::Size(300, 300);
         this->Controls->Add(this->lbl);
         this->Name = L"Form1";
         this->Text = L"Form1";
         this->Load += gcnew EventHandler(this, &Form1::Form1_Load);
         this->FormClosing += gcnew FormClosingEventHandler(this, &Form1::Form1_FormClosing);
         this->ResumeLayout(false);
         this->PerformLayout();
      }
#pragma endregion
      delegate void Handler();
      void UpdateLabel()
      {
         lbl->Text = DateTime::Now.ToString();
      }
      void Counter()
      {
         while(!done)
         {
            Thread::Sleep(1000);
            // this gives an error:
            // lbl->Text = DateTime::Now.ToString();
            lbl->Invoke(gcnew Handler(this, &Form1::UpdateLabel));
         }
      }
      void Form1_Load(Object^ sender, EventArgs^ e)
      {
         UpdateLabel();
         done = false;
         Thread^ t = gcnew Thread( gcnew ThreadStart( this, &Form1::Counter ) );
         t->Start();
      }
      void Form1_FormClosing(Object^ sender, FormClosingEventArgs^ e)
      {
         done = true;
      }
   protected:
      ~Form1()
      {
         if (components)
         {
            delete components;
         }
      }
   public:
      Form1(void)
      {
         InitializeComponent();
      }
   };
}
