在另一个线程中每隔0.5秒旋转一次图像

时间:2013-01-29 23:22:33

标签: c# wpf user-interface

我想制作一个小型的财富之轮游戏,但我在制作旋转图像的事件时遇到了问题

这是我现在的代码,但它似乎只运行_thread一次

    private readonly Timer _timer;
    private Thread _thread;
    private int _angle;

    public MainWindow()
    {
        InitializeComponent();
        _timer = new Timer(500);
        _timer.Elapsed += RotateWheelEvent;
        _timer.Start();

        _angle = 0;

        SetupRotationThread();
        _thread.Start();
    }

        public void RotateWheelEvent(object sender, EventArgs args)
    {
        _thread.Abort();
        _angle = _angle + 15;
        _thread.Start();
    }

    public void SetupRotationThread()
    {
        var rotatetransform = new RotateTransform(_angle);

        _thread = new Thread(
            delegate()
                {
                    ImageWheel.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.SystemIdle,
                        TimeSpan.FromSeconds(1),
                        new Action(
                            delegate()
                                {
                                    ImageWheel.RenderTransformOrigin = new Point(0.5, 0.5);
                                    ImageWheel.RenderTransform = rotatetransform;
                                }
                            ));
                });
    }

2 个答案:

答案 0 :(得分:3)

您每次只能使用System.Threading.Timer而不是System.Timer产生新的鞭..

        private System.Threading.Timer _timer;
        private int _angle;

        public MainWindow()
        {
            InitializeComponent();
              _timer = new System.Threading.Timer((o) => 
              {
                  _angle += 15;
                  Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate
                  {
                      ImageWheel.RenderTransformOrigin = new Point(0.5, 0.5);
                      ImageWheel.RenderTransform = new RotateTransform(_angle);
                  }); 
              }, null, 500, 500);
        }

然而,不清楚为什么你要对它进行线程化,你似乎正在启动一个只调用代码回到UI线程的线程,我没有看到这个线程需要做的任何工作。

所以我认为你需要的就是这个

private readonly Timer _timer;
private int _angle;

    public MainWindow()
    {
        InitializeComponent();
        _timer = new Timer(500);
        _timer.Elapsed += RotateWheelEvent;
        _timer.Start();
        _angle = 0;
    }

    public void RotateWheelEvent(object sender, EventArgs args)
    {
        Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate
        {
            _angle += 15;
            ImageWheel.RenderTransformOrigin = new Point(0.5, 0.5);
            ImageWheel.RenderTransform = new RotateTransform(_angle);
        }); 
    }

答案 1 :(得分:1)

最糟糕的是,您应该在代码隐藏中使用DispatcherTimer

var timer = new DispatcherTimer
{
    Interval = TimeSpan.FromSeconds(0.5)
};

timer.Tick += (s, e) => ((RotateTransform)ImageWheel.RenderTransform).Angle += 15;
timer.IsEnabled = true;

但根本没有必要。您只需在XAML中设置Storyboard即可。类似的东西:

<Image x:Name="image" RenderTransformOrigin="0.5,0.5" ...>
    <Image.RenderTransform>
        <RotateTransform/>
    </Image.RenderTransform>
    <Image.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard Duration="00:00:00.5" RepeatBehavior="Forever">
                    <DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="(Image.RenderTransform).(RotateTransform.Angle)" By="15"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Image.Triggers>
</Image>

某些语法可能不太正确(在linux上无法检查),但是你明白了。你过于复杂的事情非常简单。