all groups > dotnet general > april 2008 >
You're in the

dotnet general

group:

Help: System.Threading.Timer doesn't work!



Help: System.Threading.Timer doesn't work! Curious
4/23/2008 7:54:06 AM
dotnet general: Hi,

In my code, I observe an event. In the event handler, I have:

if (mIsMarketOpen == false)
{
// Execute "CheckHistoricalData" every 15
seconds
System.Threading.Timer timer = new
Timer(CheckHistoricalData, null, 1000, 15000);
mIsMarketOpen = true;
}

But "CheckHistoricalData" method is never executed! Anyone can tell me
Re: Help: System.Threading.Timer doesn't work! Jeroen Mostert
4/23/2008 7:06:20 PM
[quoted text, click to view]

The timer goes out of scope at the end of the block and is hence eligible
for garbage collection. You'll want to move the timer reference and creation
to outside the event handler, for example:

private readonly Timer historicalDataTimer = new Timer(CheckHistoricalData);

Then in the event handler you can do:

historicalDataTimer.Change(1000, 15000);

--
J.
AddThis Social Bookmark Button