【问题标题】:c# uwp timer alternativec# uwp timer替代
【发布时间】:2018-04-11 14:26:13
【问题描述】:

我使用 Timers 在 Unity (C#) 中编写了一些代码。后来我意识到我正在编写的平台 UWP 不能使用这些。我尝试了一些替代方案,例如 Threadpooltimers,但我无法让它们工作。还有其他替代方法或方法可以使 Threadpooltimers 工作吗?

using System.Collections;
using System.Collections.Generic;
using System.Timers;
using UnityEngine;

public class Spin : MonoBehaviour {

    // Use this for initialization
    void Start() {}

    public bool spin = false;
    public float speed = 36f;
    private static Timer aTimer;
    private static Timer bTimer;
    void StartSpin()
    {
        spin = true;

        // Create a timer and set two full rotation intervals for demo and 
        //another one to adjust the movement of the object (screen).
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2*360/speed * 1000 - 1;

        bTimer = new System.Timers.Timer();
        bTimer.Interval = 360 / speed * 1000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEventa;
        bTimer.Elapsed += OnTimedEventb;

        // Disable autoreset
        aTimer.AutoReset = false;
        bTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;
        bTimer.Enabled = true;
    }

    // At the end of Timer b, change the angle of the object (screen).
    void OnTimedEventb(object bTimer, System.EventArgs e)
    {
        transform.Rotate(Vector3.right, 45);
    }

    // At the end of Timer a, stop the spin movement and stop Timer b from resetting.
    void OnTimedEventa(object aTimer, System.EventArgs e)
    {
        spin = false;
        bTimer.AutoReset = false;
    }

    // Update is called once per frame
    void Update () 
    {
        transform.Rotate(Vector3.back, System.Convert.ToSingle(spin) * speed * Time.deltaTime);
    }
}

【问题讨论】:

标签: c# unity3d timer uwp


【解决方案1】:

使用协程或更新方法编写计时器。

IEnumerator Start() {
     while (true) {
          yield return new WaitForSeconds(1.0f);
          Debug.Log("Elapsed.");
     }
}

【讨论】:

    【解决方案2】:

    正如@MD Muzibur Ra​​hman 所述,您可以使用 DispatcherTimer。

    以下是从MSDN 截取的关于如何使用 DispatcherTimer 的代码:

    DispatcherTimer dispatcherTimer;
    DateTimeOffset startTime;
    DateTimeOffset lastTime;
    DateTimeOffset stopTime;
    int timesTicked = 1;
    int timesToTick = 10;
    
    public void DispatcherTimerSetup()
    {
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        //IsEnabled defaults to false
        TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
        startTime = DateTimeOffset.Now;
        lastTime = startTime;
        TimerLog.Text += "Calling dispatcherTimer.Start()\n";
        dispatcherTimer.Start();
        //IsEnabled should now be true after calling start
        TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
    }
    
    void dispatcherTimer_Tick(object sender, object e)
    {
        DateTimeOffset time = DateTimeOffset.Now;
        TimeSpan span = time - lastTime;
        lastTime = time;
        //Time since last tick should be very very close to Interval
        TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
        timesTicked++;
        if (timesTicked > timesToTick)
        {
            stopTime = time;
            TimerLog.Text += "Calling dispatcherTimer.Stop()\n";
            dispatcherTimer.Stop();
            //IsEnabled should now be false after calling stop
            TimerLog.Text += "dispatcherTimer.IsEnabled = " + dispatcherTimer.IsEnabled + "\n";
            span = stopTime - startTime;
            TimerLog.Text += "Total Time Start-Stop: " + span.ToString() + "\n";
        }
    }
    private void Page_Loaded_1(object sender, RoutedEventArgs e)
    {
        DispatcherTimerSetup();
    }
    

    【讨论】:

    • @Muzib 很抱歉回复晚了,但由于我的考试,我几乎没有时间从事这个项目。我正在阅读 DispatcherTimer 并尝试实施它但无济于事。到目前为止,我什至无法制作一个,因为我在命名空间/程序集引用方面遇到了麻烦。因为我几乎没有编码经验(对于这个平台和语言没有经验)。如果有人可以逐步解释如何修改我发布的代码以使其正常工作,我将永远感激不尽。
    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多