0

When I open the class Cellule.cs, a timer is created, when Cellule.cs is closed, the timer still call the form.

How can I dispose it effectively. This cause a problem because the form Cellules.Cs is opened frequently, and it makes a single call to the DB for each TimeHasChanged();

I already tried to add a Dispose() method in Btimer and setting the timer to null but it's doesn't solve the problem.

private BTimer timer;

public Cellules()
{
   timer = new BTimer(30000);
   timer.TheTimeChanged += TimeHasChanged;
}

    protected void TimeHasChanged()
    {
        controller.UpdateTimeMesPieces();
        lblTime.Text = controller.UpdateTimeClock();
    }

And heres the timer

public class BTimer
{
    private string theTime;
    private Timer timer;

    public BTimer(int interval)
    {
        timer = new Timer();
        timer.Tick += new EventHandler(Timer_Tick);
        timer.Interval = interval;
        timer.Start();
    }

    public delegate void TimerTickHandler();

    public event TimerTickHandler TheTimeChanged;

    public string TheTime
    {
        get
        {
            return theTime;
        }
        set
        {
            theTime = value;
            OnTheTimeChanged();

        }
    }

    protected void OnTheTimeChanged()
    {
        if (TheTimeChanged != null)
        {
            TheTimeChanged();
        }
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        TheTime = "Bing";
    }
}
2
  • Code doesn't compile: BTimer doesn't implement IDisposable
    – Blorgbeard
    May 7, 2015 at 19:50
  • @Blorgbeard Sorry I removed it, it was from my last attempt to solve it May 7, 2015 at 19:51

3 Answers 3

2

You do need to implement IDisposable, and in your Dispose method you need to Dispose the timer, not just set it to `null.

You should probably disassociate any events form the timer as well (although this may be unnecessary if the timer's Dispose method does this):

timer.Tick -= Timer_Tick;
timer.Dispose();
1

if a class contains a member that is disposable that class should implement IDisposable as well

Note: BTimer needs to implement IDisposable.

class Cellules : IDisposable
{
   BTimer timer // disposable member

   public void Dispose() // implementing IDisposable
   {
        timer.Dispose();
   }

}

then of course when you are done call Dispose on any instances of Cellules

in response to your EDIT

you want to implement IDisposable on the BTimer class

public class BTimer : IDisposable
{
private string theTime;
private Timer timer; 

 ........     

void Dispose()
{
    timer.Stop();
     timer.Dispose();

}

}

something I would add is a Stop method to the BTimer so you don't need to dispose to stop the timer.

1
  • It was from my last attempt to solve it, I edited the post sorry May 7, 2015 at 19:52
0

Your BTimer hasn't implemented IDisposable and you have to deregister your TheTimeChanged event after you don't need it any more.

Not the answer you're looking for? Browse other questions tagged or ask your own question.