accord-net / framework

Machine learning, computer vision, statistics and general scientific computing for .NET
http://accord-framework.net
GNU Lesser General Public License v2.1
4.46k stars 2k forks source link

HistogramBox causes accessed from a thread other than the thread error #2201

Open ddemland opened 3 years ago

ddemland commented 3 years ago

What would you like to submit? (put an 'x' inside the bracket that applies)

I am using the NuGet package in VS 2019 version 3.8.0 with Deedle version 2.2.0 in NuGet. The following code causes an accessed from a thread other than the thread error:

var dailyReturnHistogram = HistogramBox .Show( ohlcDF.FillMissing(0.0)["DailyReturn"].ValuesAll.ToArray() ).SetNumberOfBins(20);

System.Threading.Thread.Sleep(3000); dailyReturnHistogram.Invoke( new Action(() => { dailyReturnHistogram.Size = new System.Drawing.Size(700, 500); }) );

The full error is:

System.InvalidOperationException HResult=0x80131509 Message=Cross-thread operation not valid: Control 'HistogramBox' accessed from a thread other than the thread it was created on. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.Invalidate(Boolean invalidateChildren) at System.Windows.Forms.Control.Refresh() at Accord.Controls.HistogramBox.SetNumberOfBins(Int32 number) at ForeignExchangeRate.Chapter4BookExample.BookDataAnalyzer() in C:\Users\DDemland\source\repos\ForeignExchangeRate\ForeignExchangeRate\Chapter4BookExample.cs:line 59 at ForeignExchangeRate.Program.Main(String[] args) in C:\Users\DDemland\source\repos\ForeignExchangeRate\ForeignExchangeRate\Program.cs:line 17

This exception was originally thrown at this call stack: [External Code] ForeignExchangeRate.Chapter4BookExample.BookDataAnalyzer() in Chapter4BookExample.cs ForeignExchangeRate.Program.Main(string[]) in Program.cs


If I comment out the call to SetNumberOfBins as in the following:

var dailyReturnHistogram = HistogramBox .Show( ohlcDF.FillMissing(0.0)["DailyReturn"].ValuesAll.ToArray() ); //.SetNumberOfBins(20);

System.Threading.Thread.Sleep(3000); dailyReturnHistogram.Invoke( new Action(() => { dailyReturnHistogram.Size = new System.Drawing.Size(700, 500); }) );

The code does not error. What do I have to do to get this code to run?

Thank You,

David Demland