I have an UCMA console app and I want it to report "I am alive" signal by writting current timestamp to the database. To do that I use timer:
public static void Main(string[] args)
{
Random r = new Random();
System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerCallBack);
Timer TimerItem = new Timer(TimerDelegate, null, 0, r.Next(10000, 25000));
UCMASampleForwardIncomingCall ucmaSampleForwardIncomingCall = new UCMASampleForwardIncomingCall()
ucmaSampleForwardIncomingCall.Run();
}
TimerCallBack
writes the data to the database, ucmaSampleForwardIncomingCall
is the "main" method of the program, all is important at this stage that after initial configuration it waits for an event. But the code above executes the timer only once...
I was thinking about putting the ucmaSampleForwardIncomingCall
on separate thread:
public static void Main(string[] args)
{
Random r = new Random();
System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(TimerCallBack);
Timer TimerItem = new Timer(TimerDelegate, null, 0, r.Next(10000, 25000));
Thread callThread = new Thread(new ThreadStart(ucmaSampleForwardIncomingCall.Run));
callThread.Start();
but still the timer executes only once. I've read "How-to set timer" thread but I'm still confused here...
EDIT: using GC.keepAlive(TimerItem)
solved the issue.
edit 2: or not... after few events the timer stops, probably when there was exception caught in the ucmaSampleForwardIncomingCall
edit 3: currently working with:
public static void Main(string[] args)
{
Random r = new Random();
Timer TimerItem = new Timer(TimerCallBack, null, 0, r.Next(10000, 15000));
ucmaSampleForwardIncomingCall.Run();
GC.KeepAlive(TimerItem);
}
private static void TimerCallBack(Object o)
{.... more code.....}
But I'm not holding my hopes too high...