-1

I am using the following method to render a LiveChart into an image. However, I get the message that TempA is in another thread and I cannot access it. I understand this, the question is how do I get this resolved?

public static BitmapImage ChartToImage(LineSeries TempA, LineSeries TempB, LineSeries Level, List<string> Labels)
        {
          
            var thread = new Thread(() =>
            {  
            CartesianChart cartesianChart = new CartesianChart();
            
            .....

            TempA.ScalesYAt = 0;
            TempB.ScalesYAt = 0;
            Level.ScalesYAt = 1;
            
            .....

            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            return bitmapImage;
        }
patrickgc
  • 49
  • 7
  • I think you first need to understand why we _added_ the checks and exceptions in the first case: https://stackoverflow.com/a/14703806/3346583 | You should just make a (deep) copy of whatever data you want from TempA, hand in that copy and avoid any isues in the first place. – Christopher Aug 14 '21 at 14:43
  • Thank you very much for your answer. Unfortunately, this does not help me. – patrickgc Aug 14 '21 at 15:45
  • Have a look at the answer in [Pass List Between Threads](https://stackoverflow.com/a/8018068/10318835). That comes very close to your problem. – Steeeve Aug 14 '21 at 16:41
  • @Steeeve - Thank you so much! This was really difficult to understand as an amateur programmer. But now I have understood it. – patrickgc Aug 15 '21 at 09:47

1 Answers1

1

Make use of the object parameter for a Thread func. https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.-ctor

Setup a delegate for your Thread func. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-functions

Then you can use the built-in Tuple type to pass in an aggregated object of your arguments. Alternately you could create a special class to aggregate those arguments instead of using a Tuple. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples

public static BitmapImage ChartToImage(LineSeries TempA, LineSeries TempB, LineSeries Level, List<string> Labels)
    {
        //use a Tuple to aggregate your arguments
        (LineSeries, LineSeries, LineSeries, LineSeries, List<string>) data = (TempA, TempB, Level, Lables);

        //define a delegate
        delegate void ThreadWork(object data);

        //setup the delegate
        ThreadWork threadWork = new ThreadWork((o) => 
        {
            CartesianChart cartesianChart = new CartesianChart();
            .....
            var data = o as (LineSeries, LineSeries, LineSeries, LineSeries, List<string>);
            var tempA = data.Item1;
            var tempB = data.Item2;
            var level = data.Item3;

            tempA.ScalesYAt = 0;
            tempB.ScalesYAt = 0;
            level.ScalesYAt = 1;
            .....
        });

        //use the delegate and pass-in your arguments
        var thread = new Thread(threadWork(data));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        return bitmapImage;
    }
tia.misu
  • 166
  • 1
  • 6
  • I don't think that this solves the InvalidOperationException caused by accessing the control from another thread. Have a look at @Christopher's comment. – Steeeve Aug 14 '21 at 15:18