我有一个关于在代码中使用 dispatcherTimer 的问题。请看下面我的情况:

private void CheckShow(object sender, System.Windows.RoutedEventArgs e)
{
    DispatcherTimer dispatcherTimer = new DispatcherTimer();

    dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Start();
    string etime = DateTime.Now.Second.ToString();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    if(System.IO.File.Exists(@"C:\Default.xml"))
    {
        LoadingRecent.Text = "Loading Default Show...";
        LoadBar.Opacity = 100;
        string time1 = DateTime.Now.Millisecond.ToString();
        string time2 = DateTime.Now.Second.ToString();
        double huidigetijd = System.Convert.ToDouble(time2 + "." + time1);
        LoadBar.Value = huidigetijd;
        Remainingnummer.Text = Convert.ToString(10 - DateTime.Now.Second);

        string etime = DateTime.Now.Second.ToString();
        if (etime == "10")
        {
            var provider = (XmlDataProvider)this.Resources["CUEData"];
            var loadfilepath = @"C:\Default.xml";
            provider.Source = new Uri(loadfilepath, UriKind.Absolute);
            Storyboard Hoofdvenster = (Storyboard)Resources["Hoofdvenster"];
            Hoofdvenster.Begin(this, true);
        }

正如您在顶部函数中看到的那样,我启动了计时器,然后在 Tick 中,当计时器达到 10 秒时我会做一些事情。但是,我想在该 if 语句中停止调度程序计时器,但随后出现上下文错误。

那么如何在不同的函数中停止计时器呢?

更新:我试图适应你的解决方案,但我得到一个对象引用未设置为实例错误
public void CheckShow(object sender, System.Windows.RoutedEventArgs e)
{
    DispatcherTimer dispatcherTimer = new DispatcherTimer();

    dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Start();
    string etime = DateTime.Now.Second.ToString();

    if (etime == "13")
    {
        dispatcherTimer.Stop();
        LoadingRecent.Text = "You are currently working on a show. Press New or Load to create or load a different show";
        LoadBar.Opacity = 0;
    }
}
private DispatcherTimer dispatcherTimer;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    if(System.IO.File.Exists(@"C:\Default.xml"))
    {
        LoadingRecent.Text = "Loading Default Show...";
        LoadBar.Opacity = 100;
        string time1 = DateTime.Now.Millisecond.ToString();
        string time2 = DateTime.Now.Second.ToString();
        double huidigetijd = System.Convert.ToDouble(time2 + "." + time1);
        LoadBar.Value = huidigetijd;
        Remainingnummer.Text = Convert.ToString(10 - DateTime.Now.Second);

        string etime = DateTime.Now.Second.ToString();
        if (etime == "10")
        {
            var provider = (XmlDataProvider)this.Resources["CUEData"];
            var loadfilepath = @"C:\Default.xml";
            provider.Source = new Uri(loadfilepath, UriKind.Absolute);
            Storyboard Hoofdvenster = (Storyboard)Resources["Hoofdvenster"];
            Hoofdvenster.Begin(this, true);
            dispatcherTimer.Stop();
        }

最佳答案

您可以在类中使用私有(private)字段来包含 DispatcherTimer 实例。然后你可以在你的类的每个非静态方法中访问它。
编辑:添加示例
为了给您一个示例,我开发了一个简单的 WPF 应用程序,其中包含一个内容每秒更新的 TextBlock。五秒钟后,定时器关闭。这是 XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Text="{Binding Path=Counter}" />
    </Grid>
</Window>

这是代码隐藏:
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Start();
    }

    private DispatcherTimer dispatcherTimer;

    private int counter;
    public int Counter
    {
        get { return counter; }
        set
        {
            counter = value;
            OnPropertyChanged("Counter");
        }
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        Counter++;
        if (Counter == 5)
        {
            dispatcherTimer.Stop();
            dispatcherTimer = null;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler e = PropertyChanged;
        if (e != null)
        {
            e(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

希望这可以帮助。问候。

关于c# - 如何在不同的函数中使用 dispatcherTimer.Stop?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3328880/

10-09 00:17