1

嗨,我所有的简单代码都是这样的

    private void flipForeverever(object sender, EventArgs args)
    {


            moveYs = new DoubleAnimation();
            m2oveYs = new DoubleAnimation();

            try
            {
                sf.Remove(this);
                sf.Children.Clear();

            }
            catch (Exception e)
            {

            }

            if (firstPanelAngle == 360)
            {
                moveYs.To = 180;
                moveYs.From = 0;
            }
            else
            {
                moveYs.To = 360;
                moveYs.From = 180;
            }




            if (secondPanelAngle == 360)
            {
                m2oveYs.To = 180;
                m2oveYs.From = 0;
            }
            else
            {
                m2oveYs.To = 360;
                m2oveYs.From = 180;
            }



            sf = (Storyboard)FindResource("Storyboard1");
            Storyboard.SetTargetName(moveYs, "rotatePanel");
            Storyboard.SetTargetProperty(moveYs, new                                          thisPropertyPath(AxisAngleRotation3D.AngleProperty));

            Storyboard.SetTargetName(m2oveYs, "rotateSecond");
            Storyboard.SetTargetProperty(m2oveYs, new PropertyPath(AxisAngleRotation3D.AngleProperty));

            sf.Children.Add(moveYs);
            sf.Children.Add(m2oveYs);
           // sf.RepeatBehavior = RepeatBehavior.Forever;

            if (flipForever)
            {
            sf.Completed += new EventHandler(delaythespin);

                 sf.Begin(this);




        }
    }

    private void delaythespin(object sender, EventArgs args)
    {
        sf.Stop(this);

        System.Timers.Timer timer = new System.Timers.Timer(500);
        timer.Elapsed += new System.Timers.ElapsedEventHandler(flipForeverever);

        timer.Enabled = true;
         firstPanelAngle = rotatePanel.Angle;
        secondPanelAngle = rotateSecond.Angle;
        timer.Start();


    }

所以基本上我通过点击调用调用flipForeverever,它应该永远循环,直到我将flipforever设置为false......但是它给了我这个错误......

调用线程无法访问此对象,因为不同的线程拥有它。

任何帮助将不胜感激

4

1 回答 1

3

听起来您可能遇到线程关联性问题。您是否尝试过使用DispatcherTimer而不是System.Timers.Timer

private DispatcherTimer _timer;

private void GoButton_Click(object sender, RoutedEventArgs e)
{
    _timer = new DispatcherTimer();                // create timer
    _timer.Interval = new TimeSpan(0, 0, 1);       // tick every 1s
    _timer.Tick += new EventHandler(_timer_Tick);  // method to call
    _timer.Start();                                // start timer
}

void _timer_Tick(object sender, EventArgs e)
{
    // we should be on the correct thread now
    GoButton.Background = Brushes.Aqua;
}

或者,如果您需要使用 System.Timers.Timer,请在计时器触发后使用InvokeBeginInvoke进入正确的线程?

private System.Timers.Timer _timer;

private void GoButton_Click(object sender, RoutedEventArgs e)
{
    _timer = new System.Timers.Timer(1000);
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
    _timer.Start();
}

void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // use Dispatcher.Invoke on the UI object that you want to modify
    // to get on the correct thread for that UI object
    GoButton.Dispatcher.Invoke((ThreadStart)(() =>
    {
        GoButton.Background = Brushes.Aqua;
    }));
}
于 2012-05-30T01:52:29.567 回答