从主GUI线程中检索信息

时间:2015-09-08 12:09:11

标签: wpf multithreading user-interface

我正在尝试检索Ellipse的X和Y坐标。检索通过计时器和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);

    }

我的问题是我无法从GUI检索坐标。

我收到错误

  

"调用线程无法访问此对象,因为另一个线程拥有它" 。

我想在每次修改时将Ellipse的坐标保存在不同的类中,Timer可以定期访问值来读取它们。

我的问题是:

a)有没有办法直接从GUI线程获取我需要的信息;

b)如果没有,那么我怎样才能将我的画布中的椭圆x和y位置传递给我制作的类

1 个答案:

答案 0 :(得分:0)

您可以使用窗口的Dispatcher属性并调用Invoke方法从GUI线程运行操作