Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
So, I need a basic wpf application with navigation. For navigation I chose UserControl. There is TextBlock and Button in UserControl, call it for example Home. For Timer I created static class. Here is the following Timer.cs code:
C#
namespace WpfApplication12
{
    public static class Timer
    {
        static DispatcherTimer dt = new DispatcherTimer();
        static Stopwatch sw = new Stopwatch();
        static string currenttime = string.Empty;

        static Timer()
        {
            dt.Tick += new EventHandler(dt_Tick);
            dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
        }

        public static string Time
        {
            get
            {
                return currenttime;
            }
        }

        private static void dt_Tick(object sender, EventArgs e)
        {
            if (sw.IsRunning)
            {
                TimeSpan ts = sw.Elapsed;
                currenttime = string.Format("{0.00}:{1.00}.{2.00}",
                    ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            }

        }

        public static void StartTime()
        {
            sw.Start();
            dt.Start();
        }

        public static void StopTime()
        {
            if (sw.IsRunning)
                sw.Stop();
        }
    }
}


So, the problem is when I try to display Timer time on the TextBlock in Home.xaml(UserControl), application stopping and closing every time.

What I have tried:

Here is the Home.xaml(UserControl) code:
<UserControl x:Class="WpfApplication12.Home"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Button Content="Start" Name="btn_Start" Grid.Row="0" Click="btn_Start_Click"/>
        <TextBlock Text="" Name="Display" FontSize="36" Grid.Row="1"/>
    </Grid>
</UserControl>

Home.xaml.cs:
namespace WpfApplication12
{
    public partial class Home : UserControl
    {
        public Home()
        {
            InitializeComponent();
        }

        private void btn_Start_Click(object sender, RoutedEventArgs e)
        {
            Timer.StartTime();
            Display.Text = Timer.Time;
        }
    }
}

MainWindow.xaml:
<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication12"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <local:Home Width="200"/>
        </ListBox>
    </Grid>
</Window>
Posted
Updated 4-Apr-16 19:23pm
Comments
Richard Deeming 4-Apr-16 9:40am    
Your application is terminating because something is throwing an exception which your code doesn't handle.

You need to debug the application and get the details of the exception. That will tell you what the problem is.

If you don't understand the exception, then update your question and post the full details of the exception, including any inner exceptions. Remember to indicate which line of code the exception is thrown from.
Member 12355239 5-Apr-16 14:24pm    
Yeap, thanks, i clicked debug program, it shwoed me what's problem with and that was the line of the Timer time. Just wrote once again timer and it's working
Gautham Prabhu K 4-Apr-16 10:53am    
What is the exception or error your getting?
Member 12355239 5-Apr-16 14:25pm    
got SystemFormatException, but I created this right way
Sergey Alexandrovich Kryukov 4-Apr-16 14:18pm    
What is the purpose of this timer? It does not seem to do anything useful. What do you want to achieve.
—SA

1 solution

C#
 public partial class StopWatchDemo : Window
{
    DispatcherTimer dt = new DispatcherTimer();
    Stopwatch stopWatch = new Stopwatch();
    string currentTime = string.Empty;
    public StopWatchDemo()
    {
        InitializeComponent();
        dt.Tick += new EventHandler(dt_Tick);
        dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
    }

    void dt_Tick(object sender, EventArgs e)
    {
        if (stopWatch.IsRunning)
        {
            TimeSpan ts = stopWatch.Elapsed;
            currentTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            ClockTextBlock.Text = currentTime;
        }
    }
    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        stopWatch.Start();
        dt.Start();
    }


}
 
Share this answer
 
Comments
Member 12355239 5-Apr-16 14:29pm    
Yeap, thanks. The line with currentTime format was wrong. Just wrote once again and it's working.
Sergey Alexandrovich Kryukov 5-Apr-16 14:45pm    
That's right, a stopwatch. Using a timer is silly... :-(
—SA
Member 12355239 5-Apr-16 14:57pm    
Maybe it's. Actually I'm new in WPF and C#. Just didn't see any ways to solve the purpose which I needed
Sergey Alexandrovich Kryukov 5-Apr-16 15:04pm    
It's good that now you know.
I generally would advise to avoid timers by all means. In most cases, presence of a timer is the indication of a bad design. In cases where it's absolutely required for solving some particular problem, it could be the indication that the "bigger technology" or organization you are serving or participating in is incorrectly designed.

Good luck.
—SA

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