//---------------------------------------------------------------------------
/*
Remote Train Control Program for Windows

© Copyright 2015 by Mark DiVecchio

    This file is part of Remote Train Control.

    Remote Train Control is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Remote Train Control is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

*/
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// This thread delays a message and then resends it
// No VCL calls are used in this thread
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Delay_Win_Msg.h"
#include "Message_Thread.h"
#include "Train_Control.h"
#include "Debug_Controls.h"

#pragma package(smart_init)

//---------------------------------------------------------------------------
//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall Delay_Windows_Message::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------
__fastcall TDelay_Windows_Message_Thread::TDelay_Windows_Message_Thread(bool CreateSuspended)
	: TThread(CreateSuspended)
{
assert(false);	// never called since I used the overloaded constructor below
}
//---------------------------------------------------------------------------
__fastcall TDelay_Windows_Message_Thread::TDelay_Windows_Message_Thread(bool CreateSuspended, TForm * Callingthis, HWND DestWindow,
	int Msg_Delay, UINT Msg, WPARAM wparam, LPARAM lparam)
	: TThread(CreateSuspended)
{
My_Msg_Delay = Msg_Delay;
My_Msg = Msg;
My_wparam = wparam;
My_lparam = lparam;
My_DestWindow = DestWindow;

My_Callingthis = Callingthis;
My_DestWindow = DestWindow;

// FreeOnTerminate is set so that parent does not have to worry about it
FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall TDelay_Windows_Message_Thread::Execute()
{
// Calls to Sleep() are ok in this thread
// NO VCL Calls in this thread
if (Terminated) {
	return;						// in case we are terminated
    }
//
Sleep(My_Msg_Delay);	// Delay in milliseconds
// Send Msg message to Parent after Msg_Delay
if (!PostMessage(My_DestWindow, My_Msg, My_wparam, My_lparam)) {
	// Message did not post correctly
    TMessageDisplayThread * pThd = new TMessageDisplayThread(false, My_Callingthis, My_DestWindow, String("Error"), String("PostMessage Failure"), String("in Delay_Windows_Message"), 1000);
    assert(pThd != NULL);
	return;
	}
// otherwise message posted correctly
}
//---------------------------------------------------------------------------

