title: Cannot access a disposed object
body: <p>Cannot access a disposed object</p>
  
  <p>Anyone know why I would get this error "Cannot access a disposed object" on this line of code</p>
  
  <pre><code>Invoke(new UpdateTimerDelegate(UpdateTimer), new object[] { null });
  </code></pre>
  
  <p>Hit start over then either accept / reject / or close the form -- seems to happen when closing the form</p>
  
  <p>the error happens when you bring the form up again</p>
  
  <pre><code>using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace TimerTester
  {
      public class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void StartOverBtn_Click(object sender, EventArgs e)
          {
              ShowForm2();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              ShowForm2();
          }
  
          private void ShowForm2()
          {
              Form2 f2;
  
              f2 = new Form2(10000);
              f2.Owner = this;
              switch (f2.ShowDialog())
              {
                  case DialogResult.Yes:
                      break;
                  case DialogResult.No:
                      break;
                  default:
                      break;
              }
          }
  
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing)
          {
              if (disposing &amp;&amp; (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Windows Form Designer generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {
              this.StartOverBtn = new System.Windows.Forms.Button();
              this.SuspendLayout();
              // 
              // StartOverBtn
              // 
              this.StartOverBtn.Location = new System.Drawing.Point(33, 33);
              this.StartOverBtn.Name = "StartOverBtn";
              this.StartOverBtn.Size = new System.Drawing.Size(75, 23);
              this.StartOverBtn.TabIndex = 0;
              this.StartOverBtn.Text = "Start Over";
              this.StartOverBtn.UseVisualStyleBackColor = true;
              this.StartOverBtn.Click += new System.EventHandler(this.StartOverBtn_Click);
              // 
              // Form1
              // 
              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
              this.ClientSize = new System.Drawing.Size(292, 266);
              this.Controls.Add(this.StartOverBtn);
              this.Name = "Form1";
              this.Text = "Form1";
              this.Load += new System.EventHandler(this.Form1_Load);
              this.ResumeLayout(false);
  
          }
  
          #endregion
  
          private System.Windows.Forms.Button StartOverBtn;
      }
  }
  </code></pre>
  
  <h2>form2</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Diagnostics;
  
  namespace TimerTester
  {
      public class Form2 : Form
      {
          public Form2(int timeoutSecondsForAcceptCall)
          {
              this.timeoutSecondsForAcceptCall = timeoutSecondsForAcceptCall;
              InitializeComponent();
              tUpdateTimer = new System.Threading.Timer(new System.Threading.TimerCallback(UpdateTimer), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
          }
  
          DateTime dtCreate = DateTime.Now;
          int timeoutSecondsForAcceptCall = 99999;
          System.Threading.Timer tUpdateTimer;
  
          private delegate void UpdateTimerDelegate(object obj);
          void UpdateTimer(object obj)
          {
              if (InvokeRequired == true)
              {
                  Invoke(new UpdateTimerDelegate(UpdateTimer), new object[] { null });
              }
              else
              {
                  TimeSpan ts = DateTime.Now - dtCreate;
                  if (ts.TotalSeconds &gt; timeoutSecondsForAcceptCall)
                  {
                      DialogResult = DialogResult.Cancel;
                      return;
                  }
                  TimeSpan timeleft = TimeSpan.FromSeconds(timeoutSecondsForAcceptCall) - ts;
                  label1.Text = FormatTimeSpan(timeleft);
              }
          }
  
          private string FormatTimeSpan(TimeSpan ts)
          {
              return ((int)ts.TotalMinutes).ToString().PadLeft(2, '0') + ":" + ((int)ts.Seconds).ToString().PadLeft(2, '0');
          }
  
  
          private void btnAccept_Click(object sender, EventArgs e)
          {
              DialogResult = DialogResult.Yes;
          }
  
          private void btnReject_Click(object sender, EventArgs e)
          {
              DialogResult = DialogResult.No;
          }
  
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing)
          {
              if (disposing &amp;&amp; (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Windows Form Designer generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {
              this.btnAccept = new System.Windows.Forms.Button();
              this.btnReject = new System.Windows.Forms.Button();
              this.label1 = new System.Windows.Forms.Label();
              this.SuspendLayout();
              // 
              // btnAccept
              // 
              this.btnAccept.Location = new System.Drawing.Point(31, 30);
              this.btnAccept.Name = "btnAccept";
              this.btnAccept.Size = new System.Drawing.Size(75, 23);
              this.btnAccept.TabIndex = 0;
              this.btnAccept.Text = "Accept";
              this.btnAccept.UseVisualStyleBackColor = true;
              this.btnAccept.Click += new System.EventHandler(this.btnAccept_Click);
              // 
              // btnReject
              // 
              this.btnReject.Location = new System.Drawing.Point(140, 29);
              this.btnReject.Name = "btnReject";
              this.btnReject.Size = new System.Drawing.Size(75, 23);
              this.btnReject.TabIndex = 1;
              this.btnReject.Text = "Reject";
              this.btnReject.UseVisualStyleBackColor = true;
              this.btnReject.Click += new System.EventHandler(this.btnReject_Click);
              // 
              // label1
              // 
              this.label1.AutoSize = true;
              this.label1.Location = new System.Drawing.Point(31, 80);
              this.label1.Name = "label1";
              this.label1.Size = new System.Drawing.Size(35, 13);
              this.label1.TabIndex = 2;
              this.label1.Text = "label1";
              // 
              // Form2
              // 
              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
              this.ClientSize = new System.Drawing.Size(240, 117);
              this.Controls.Add(this.label1);
              this.Controls.Add(this.btnReject);
              this.Controls.Add(this.btnAccept);
              this.Name = "Form2";
              this.Text = "Form2";
              this.ResumeLayout(false);
              this.PerformLayout();
  
          }
  
          #endregion
  
          private System.Windows.Forms.Button btnAccept;
          private System.Windows.Forms.Button btnReject;
          private System.Windows.Forms.Label label1;
      }
  }
  </code></pre>