Your code updates the control twice:
- Using the call to
Invoke
, which delegates another call toOnHistoryUpdate
. This is safe becauseInvoke
marshals execution to the UI thread. - After the call to
Invoke
.Invoke
executes synchronously and when it returns your code will callLoadTimeSeries
a second time, this time unsafely (it doesn't checkInvokeRequired
again).
I would change the method as below, and also consider using BeginInvoke
instead so the worker thread doesn't block.
private void OnHistoryUpdate(object sender)
{
Console.WriteLine("<< Invoke? " + this.InvokeRequired + " " + Thread.CurrentThread.ManagedThreadId );
if (this.InvokeRequired)
this.Invoke(new Action<object>(OnHistoryUpdate), sender);
else
LoadTimeSeries(this.Interval);
Console.WriteLine(">> Invoke? " + this.InvokeRequired + " " + Thread.CurrentThread.ManagedThreadId);
}