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

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

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
anything I did wrong? Thanks!
 
Curious said:
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
anything I did wrong? Thanks!

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top