المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Thread sychronization



C# Programming
02-14-2010, 01:41 AM
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 (Dispatcher.CurrentDispatcher != _dispatcher)
// _dispatcher is a reference to the Dispatcher under wich the Timer was instantated
{
_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?modified on Friday, February 12, 2010 11:29 AM