Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everybody,
I have a question.
Why is my measured time still 0?? I created a stopwatch´s measuring time in milliseconds. I think I'm making a mistake converting to time format. All this in WPF Application. I apologize in advance for mixing multiple languages.
I have no idea if I am making a mistake in Stop() or somewhere else. Before I wanted to convert time to MessageBox.Show("Tvůj čas je "+ trvani.ToString("mm\:ss\.ff")); so time showed me

Can anyone tell me a mistake just by reading the code?

I'll be glad for any advice

Thank you

What I have tried:

public partial class hra : Window
    {
        Stopwatch mt = new Stopwatch();
        DispatcherTimer posunTimer = new DispatcherTimer();
        decimal trvani = new decimal();
        
        
        public hra()
        {
            InitializeComponent();
            
            posunTimer.Tick += posunTimer_Tick;
            posunTimer.Interval = TimeSpan.FromSeconds(1);
            
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mt.Start();
            timer.Value = 0;
            posunTimer.Start();
            Random prvni = new Random();
            Random druhy = new Random();
            int maxprvni = 10;
            int maxdruhy = 10;
            int prvnic = prvni.Next(1, maxprvni);
            int druhyc = druhy.Next(2, maxdruhy);
            int total = (prvnic + druhyc);
            prvnit.Text = prvnic.ToString();
            druhyt.Text = druhyc.ToString();
            vysledek.Text = total.ToString();
        } 

        void posunTimer_Tick(object sender, object e)
        {
            timer.Value += 1;
            if (timer.Value >= timer.Maximum)
            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                pocetchyb.Text = poch.ToString();
                if (poch == 4)
                {

                    MessageBox.Show("Konec hry!");
                }
            }
        }

        
        private int poch;

        private void kontrola_Click(object sender, RoutedEventArgs e)
        {
            
            timer.Value += 1;
            posunTimer.Start();
            if (vysledekhrac.Text == vysledek.Text && timer.Value < timer.Maximum)
            {
                posunTimer.Stop();
                hlaska.Text = "Správně";
            }
            else if (timer.Value >= timer.Maximum)

            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                pocetchyb.Text = poch.ToString();
                if (poch == 4)
                {
                    mt.Stop();
                    MessageBox.Show("Konec hry!");
                }
            }
            else
            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                pocetchyb.Text = poch.ToString();
                if (poch == 4)
                {
                    mt.Stop();
                    MessageBox.Show("Konec hry!");
                }
            }
            Random prvni = new Random();
            Random druhy = new Random();
            int maxprvni = 10;
            int maxdruhy = 10;
            int prvnic = prvni.Next(1, maxprvni);
            int druhyc = druhy.Next(2, maxdruhy);
            int total = (prvnic + druhyc);
            prvnit.Text = prvnic.ToString();
            druhyt.Text = druhyc.ToString();
            vysledek.Text = total.ToString();
            vysledekhrac.Text = String.Empty;
            timer.Value = 0;
            posunTimer.Start();
        }
        string path = @"C:\\Žebříček.txt";
        private void konec_Click(object sender, RoutedEventArgs e)
        {
            
            if (mt.IsRunning==true)
            {
                mt.Stop();
            }
            TimeSpan trvani = new TimeSpan(mt.ElapsedMilliseconds);
            MessageBox.Show("Tvůj čas je "+ trvani.ToString("mm\\:ss\\.ff"));
            if (!File.Exists(path))
            {
                StreamWriter str = File.CreateText(path);
                str.WriteLine(trvani);
            }
            else if (File.Exists(path))
            {
                var str = new StreamWriter(path);
                str.WriteLine(trvani);
            }
                
            this.Close();
        }
    }
}
Posted
Updated 13-Apr-21 0:33am

1 solution

The TimeSpan constructor you're calling expects the number of ticks, but you are passing the number of milliseconds.

TimeSpan Constructor (System) | Microsoft Docs[^]

One tick is 100 nanoseconds. There are 10,000 ticks in a millisecond. With two decimal places for your fractional seconds, you would need to run for 100 seconds to see any value in your output.

To get the correct output, use TimeSpan.FromMilliseconds instead:
C#
TimeSpan trvani = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
TimeSpan.FromMilliseconds(Double) Method (System) | Microsoft Docs[^]
 
Share this answer
 
Comments
dejf111 13-Apr-21 6:37am    
I wish I could hug you :D I've been messing around with this crap for two days, thank you very much!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900