0

I am trying to retrieve X and Y coordinates of a Ellipse . The retrieving is done through a Timer and a EllapsedEventHandler :

public void record(object sender, ElapsedEventArgs args)
    {

        DateTime t = args.SignalTime; // Take the time the tick was done
        Point ellipseCoordiante = new Point(Canvas.GetLeft(observee.StimulyEllipse1), Canvas.GetTop(observee.StimulyEllipse1));  // Parse coordinates from StimulyWindow to service and then to thread where they will be recorded into the log file
        Point controller1 = new Point(Canvas.GetLeft(observee.Pointer1), Canvas.GetTop(observee.Pointer1));
        Point controller2 = new Point(Canvas.GetLeft(observee.Pointer2), Canvas.GetTop(observee.Pointer2));
        string[] toWrite = new string[] { t.Ticks.ToString(), " ", watch.Elapsed.ToString(), " ", ellipseCoordiante.ToString(), " ", controller1.ToString(), " ", controller2.ToString() };

        System.IO.File.WriteAllLines(logPath, toWrite);

    }

My problem is that I can not retrieve the coordinates from the GUI.

I get the error

" The calling thread cannot access this object because a different thread owns it " .

I was thinking of saving the coordinates of the Ellipse in a different class each time they get modified , and the Timer can periodically acces the values to read them .

My questions are:

a) Is there a way to get the info I need directly from the GUI thread ;

b) If not , then how could I pass the Ellipse x and y position in my Canvas to the class I make

Community
  • 1
  • 1
Iustinian Olaru
  • 1,231
  • 1
  • 13
  • 33

1 Answers1

0

You can use the Dispatcher property of the window and call the Invoke method to run an action from the GUI thread

Mattia Magosso
  • 503
  • 2
  • 9
  • Yes , I am doing that now . My problem is how to call the method for the Dispatcher to pass the information further . The size of the Ellipse is not changing and I can't seem to find any other event for the Positon changind.. – Iustinian Olaru Sep 08 '15 at 13:12
  • If you use a lambda function with the Dispatcher.Invoke in your record(object sender, ElapsedEventArgs args) method you can set the values of the properties in some local variable and use them after the lambda – Mattia Magosso Sep 08 '15 at 14:43