2015-09-08 61 views
0

我想检索Ellipse的X和Y坐标。检索通过定时器和做了EllapsedEventHandler从主GUI线程检索信息

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可以定期访问这些值以读取它们。

我的问题是:

一)有没有办法让我直接从需要GUI线程的信息;

b)若没有,那么我怎么能传递我的画布椭圆x和y位置I类做

+0

你使用什么Timer类? –

+0

是的,只需使用[DispatcherTimer](https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v = vs.110).aspx) – GazTheDestroyer

+0

我正在使用系统。 Timers.Timer。 – BigBadCoder

回答

0

您可以使用窗口的调度属性并调用Invoke方法来运行一个动作从GUI线程

+0

是的,我现在正在这样做。我的问题是如何调用Dispatcher的方法来进一步传递信息。 Ellipse的大小没有变化,我似乎无法找到Positon changind的任何其他事件。 – BigBadCoder

+0

如果您在记录(对象发件人,ElapsedEventArgs args)方法中使用带有Dispatcher.Invoke的lambda函数,您可以在某些局部变量中设置属性的值,并在lambda后使用它们 –