So i've attempted to modify the code block posted by Sriram Sakthivel here:
C#: How to start a thread at a specific time
public class Program
static Boolean checkIn = false;
public static void checkInCycle()
checkIn = SynchronousSocketClient.StartClient();
if(checkIn == false)
// Use TimeSpan constructor to specify:
// ... Days, hours, minutes, seconds, milliseconds.
TimeSpan span = new TimeSpan(00, 00, 30);
DateTime current = DateTime.Now;
TimeSpan triggerTime = current.TimeOfDay + span;
if(checkIn == true)
//Do some processing
public static void SetUpTimer(TimeSpan alertTime)
//DateTime current = DateTime.Now;
//TimeSpan timeToGo = alertTime - current.TimeOfDay;
TimeSpan timeToGo = alertTime;
Console.WriteLine("Checking in at: " + timeToGo);
if (timeToGo < TimeSpan.Zero)
return; //time already passed
System.Threading.Timer timer = new System.Threading.Timer(x =>
Console.WriteLine("Running CheckIn Cycle");
}, null, timeToGo, Timeout.InfiniteTimeSpan);
public static int Main(String args)
Console.WriteLine("End of Program Reached");
This is happening because at the end of your execution of the main() method you are losing the reference to your
new System.Threading.Timer call does not block the main thread while it waits to run its code.
In the above reference (second note under Remarks), the documentation explains that a reference must be maintained (as with all managed code) for it not to be disposed of by the Garbage Collector.
As long as you are using a Timer, you must keep a reference to it. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.
The way you can keep this simple program is if you use a
System.Threading.Monitor and perform a
Wait() directly after the
checkInCycle() function call in the
main() function. Then
Pulse() when you want to proceed.
You may want to read up on thread synchronization or concurrency further if you are curious about any more in-depth topics on performing asynchronous calls or operating in a mutli-threaded environment.
*edited for clarification.