Uros Bregar

Uros Bregar

  • NA
  • 15
  • 0

Thread synchronization

Feb 12 2010 11:48 AM

Hi
I created a custom timer class (MyTimer). The class is basicaly just a wrapper that internaly uses System.Threading.Timer. Class MyTimer will be used in a windows service app. Since the Timer class signals it events on the background thread, I am using Dispatcher.Invoke to marshal the requests to the current thread. Below is the event handler for the internaly used System.Threading.Timer.
 
private void timer_Elapsed(object state)
{
if (_synchronizeEvent && Dispatcher.CurrentDispatcher != _dispatcher)
{
//_dispatcher is a reference to the Dispatcher under wich the timer was instantiated
_dispatcher.Invoke(
new System.Threading.ThreadStart(delegate
{
onTick();
}), DispatcherPriority.Send); 
}
else
{
onTick();
}
}
 

private
void onTick()
{
if (Tick != null)
{
Tick(
this, new EventArgs());
}
}
 
The problem is that in some casses the _dispatcher.Thread is in stopped or WaitSleepJoin state, and the onTick() method doesn't execute. Does anybody has a solution to this problem? Any ideas will be appreciated.

Uroš