开发者

How to use dispatcherTimer.Stop in a different function?

开发者 https://www.devze.com 2023-01-08 14:05 出处:网络
I have a question regarding the use of dispatcherTimer in code. Please look at my situation below: private void CheckShow(object sender, System.Windows.RoutedEventArgs e)

I have a question regarding the use of dispatcherTimer in code. Please look at my situation below:

private void CheckShow(object sender, System.Windows.RoutedEventArgs e)
{
    DispatcherTimer dispatche开发者_JS百科rTimer = 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);
        }

As you can see in the top function I start the timer and then in the Tick I do some stuff when the timer reaches ten seconds. However I want to stop the dispatchertimer in that if statement but then I get a context error.

So how do I stop the timer in a different function?

Update: I tried to fit in your solution but I get an object reference not set to an instance error

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(); 
        }


You can use a private field inside your class to contain the DispatcherTimer instance. Then you can access it in every non-static method of your class.

EDIT: Adding sample

To give you a sample, I developed a simple WPF application containing a TextBlock which content is update every second. After five seconds, the timer is switched off. This is the 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>


And this is the code-behind:

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));
        }
    }
}


Hope this helps. Regards.


this works :)

    public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();


    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
             //Do Something
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        Dispatcher.InvokeShutdown();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号