Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Cannot access a disposed object, when trying to hide a form
    primarykey
    data
    text
    <p>I have a MDI based charting application. Each child form is a form that contains a chart. Each chart has its own data queue and a dedicated worker thread.</p> <p>In the worker thread DoWork method, I have a while loop controlled by a bool "IsChartActive", which will plot the data on the graph. To plot the data, I call Invoke on the chartForm itself and pass in a delegate to the method that should update the chart.</p> <p>When the user closes a chart form, instead of actually closing it, I stop the chart updates and want to hide the chart till the user wants to see it again.</p> <p>To do this I handle the FormClosing event, set Cancel = true, set "IsChartActive = false" and call ChartForm.Hide().</p> <p>But after this I get the "Cannot access a disposed object" exception in the DoWork method ( I assume this happens due to the background thread being already at the invoke statement when I set the "IsChartActive = false", so it will see the change in the next iteration), when calling invoke on the chart.</p> <p>Why am I getting this exception, even though the chart is not actually disposed but only hidden?</p> <pre><code>public void StopChartUpdates() { IsActive = false; updateChartThread.CancelAsync(); } private void updateChartThread_DoWork(object sender, DoWorkEventArgs e) { while (IsActive &amp;&amp; !updateChartThread.CancellationPending) { try { ChartView.Invoke(new Action(InvokeOnUiThread)); } catch (ObjectDisposedException ex) { //getting this error when calling invoke on ChartView Trace.WriteLine("Exception: " + ex.Message); } Thread.Sleep(UpdateFrequency); } } private void InvokeOnUiThread() { try { //update the chart } catch (Exception ex) { Trace.WriteLine("Exception: " + ex.Message); } } private void ChartView_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) { StopChartUpdates(); if (e.CloseReason == CloseReason.UserClosing) { ChartView.Hide(); e.Cancel = true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload