0

I am trying to make a program with 2 timers in it running at different intervals. Currently I have 1 timer working fine and I need to have another one running. My code for the first timer that works looks like this:

    private void startButton_Click(object sender, RoutedEventArgs e)
    {
        Random rand = new Random();
        int ranMin = rand.Next(1,24);
        int ranSec = rand.Next(0, 59);


        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
        dispatcherTimer.Start();

        min.Content = ranMin;
        sec.Content = ranSec;

        openP();

    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        **code for timer in here
    }

This works fine, but now I need another timer running at a 1second interval with different code and when I try to duplicate this by just making all the dispatcherTimer into dispatcherTimer2 I am running into errors.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
crams
  • 325
  • 1
  • 6
  • 16
  • 1
    Tip to help yourself, if you find you typing the words running into errors and don't put the errors in the question you are making it hard to answer you , and less likely you will get an answer. so please put the errors you are getting into your question. – Mark Hall Sep 19 '14 at 02:52
  • Please avoid tags in title and thank you notes in your posts. Also note that "visual studio" tag is for integration with VS or other features directly related to VS and not for code that one can write in VS (as it will apply to 99% of questions on this site). – Alexei Levenkov Sep 19 '14 at 03:12
  • Side note: the way `Random` is used in sample is random and will not produce random results. Please check out top rated question on issues with your code: [Random not random](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Alexei Levenkov Sep 19 '14 at 03:13

1 Answers1

4

I am not sure what you were doing (you should post the error), but the following works with your supplied code:

    private void startButton_Click(object sender, RoutedEventArgs e)
        {
            Random rand = new Random();
            int ranMin = rand.Next(1,24);
            int ranSec = rand.Next(0, 59);


            System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
            dispatcherTimer.Start();

            // New timer
            System.Windows.Threading.DispatcherTimer dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
            dispatcherTimer2.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer2.Start();

            min.Content = ranMin;
            sec.Content = ranSec;

            openP();

        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            //code for timer in here
        }

        private void dispatcherTimer2_Tick(object sender, EventArgs e)
        {
           //code for timer2 in here
        }
JuStDaN
  • 449
  • 2
  • 7
  • Ah, that works I was making a dumb mistake, the line dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick); the last bit was giving an error saying it didnt exist but that was because I did not have the private void line below.... Anyway it works great now, thank you for the help – crams Sep 19 '14 at 03:07
  • @user3524727 These things happen! – JuStDaN Sep 19 '14 at 03:10