If do you want do add one Timer Listening during on current thread of your application, this class auxiliate this job. TimerListen class has a decople code that can be used with asynchronously or synchronously (by SynchronizationContext) with User Interface.
The approach is very simple: define on method that needs to be executed frequently (for example, between one-one second steps or ten-and-ten seconds steps etc.), like bellow:
public static void MethodToExecuteFrequently()
{
System.Windows.Forms.MessageBox.Show("Ends cycle of timer!");
}
The instance of TimerListen is define as a field class:
public static TimerListen listening = new TimerListen(1000, new Action(() => MethodToExecuteFrequently()))
The developer can instance with sychronized a UI Thread:
//This object was assigned at UI layer:
SynchronizationContext uiThread = SynchronizationContext.Current;
//The TimerListen object with parameters, including the threadContext:
public static TimerListen listening = new TimerListen(1000, new Tuple<Action, SynchronizationContext>(
()=>Method, uiThread));
And define the starts and the end of listening:
static void Main()
{
listening.Start();
//Batch and long processing to be execute...
listening.Stop();
}
The Timer Listen class enjoys features of .NET Framework Timer class, that has a pooling thread, based on Rounding Robing Scheduling