# C# - Please Help And Critique



## char[] rager (Jul 31, 2010)

Before Fall semester starts at my university, a professor has already given us "homework" to do before we start his class. He told us to make something simple, as long as it had a GUI. So, I made a timer/stopwatch application. I would appreciate if you guys could critique it before I turn it in, because I want to get a good grade on it. The program attachment .zip is at the bottom of this post, it has two files in it:


"time.exe" - This is the program
"sound.wav" - This is the crude recording of a wind-up timer going off that is used in "time.exe"

*Second:*

I feel that my stopwatch is a little slow in terms of actually counting the centiseconds. I have my timerStopwatch interval at 10ms (1cs), and I base my seconds, minutes, and hours off of the centisecond timer. I have included the code that controls that, maybe you guys could help me fix it?


```
private void timerStopwatch_Tick(object sender, EventArgs e)
        {
            stopwatchCentiseconds++;
            textBoxStopwatchCentiseconds.Text = Convert.ToString(stopwatchCentiseconds);

            if (stopwatchCentiseconds == 99)
            {
                stopwatchSeconds++;
                textBoxStopwatchSeconds.Text = Convert.ToString(stopwatchSeconds);

                if (stopwatchSeconds == 60)
                {
                    stopwatchMinutes++;
                    textBoxStopwatchMinutes.Text = Convert.ToString(stopwatchMinutes);

                    if (stopwatchMinutes == 60)
                    {
                        stopwatchHours++;
                        textBoxStopwatchHours.Text = Convert.ToString(stopwatchHours);

                        stopwatchMinutes = 0;
                        textBoxStopwatchMinutes.Text = Convert.ToString(0);
                    }

                    stopwatchSeconds = 0;
                    textBoxStopwatchSeconds.Text = Convert.ToString(0);
                }
                
                stopwatchCentiseconds = 0;
            }
        }
```

Here is the .zip file, it will most likely require you to have the .NET Framework 4 Client Profile installed on your computer to work:


----------



## A Cheese Danish (Jul 31, 2010)

Yes, your stopwatch is slower. Although with that code I can not be of any assistance. Sorry 

Bumpage for more assistance


----------



## Perseid (Jul 31, 2010)

There really isn't any need for all the variables. You could store only the ticks and use the DateTime functions to get the seconds etc. This would give you only one variable and would eliminate the nested ifs.

It would also, unfortunately, make it some pretty unimpressive homework, though. LOL

http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx


----------



## FordGT90Concept (Jul 31, 2010)

Yeah, I would recommend the same.  Use DateTime (the system clock) instead of relying on a timer to keep track of time.  Although you will need a separate thread to update your GUI.  Keep the timer you got there now and replace the innards with DateTime (DateTime.Now - start = duration as a TimeSpan) and reduce the interval on the timer to between 100-1000 ms.  Use the timer to update your GUI.


----------



## char[] rager (Aug 1, 2010)

I guess I will have to teach myself to multi-thread then, because we have not been taught yet.
I told myself that I would not use the _DateTime_ or the _System.Diagnostics.Stopwatch_ classes (yes, I knew about them before making this thread) because I wanted to make it work by just using one timer.

However, I guess I did, but it does not work as well as I thought it would. I do have about three works to work on this as well to make it better, so I will make it better.

Thank You all for responding 

Edit:  I forgot to thank everyone that responded. I am sorry, I should have earlier because my own signature contradicted my lateness.


----------



## FordGT90Concept (Aug 2, 2010)

A Timer (which is probably what StopWatch is based on) does create and manage a separate thread.  A Timer would be the easiest way to update the GUI without locking the GUI up.


----------

