Moin,
das wäre mein Vorschlag:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
aTimer = new System.Timers.Timer();
aTimer.Interval = 1;
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
}
private System.Timers.Timer aTimer;
private int zs = 0, sek = 0, min = 0, h = 0;
private bool pruef = true;
private void B_zwz_Click(object sender, RoutedEventArgs e)
{
ListBox1.Items.Add(L_Ausgabe.Content);
}
private void B_zurueck_Click(object sender, RoutedEventArgs e)
{
aTimer.Stop();
B_Start.Content = "Start";
L_Ausgabe.Content = " 0: 0 :0 : 0 ";
ListBox1.Items.Clear();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if (pruef)
{
aTimer.Enabled = true;
aTimer.Start();
B_Start.Content = "Stop";
pruef = false;
}
else
{
aTimer.Stop();
B_Start.Content = "Start";
pruef = true;
}
}
private void changeLabel() {
L_Ausgabe.Content = h + " : " + min + " : " + sek + " : " + zs;
}
private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
try
{
zs++;
if (zs == 10)
{
sek++;
zs = 0;
}
else if (sek == 60)
{
sek = 0;
min++;
}
else if (min == 60)
{
min = 0;
h++;
}
else if (h == 24)
{
h = 0;
}
this.L_Ausgabe.Dispatcher.Invoke(changeLabel);
}
catch
{
return;
}
}
}
}
--------------------------------------------------------------------------------------------------------
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Label x:Name="L_Ausgabe" Content=""0 : 0 : 0 : 0"" HorizontalAlignment="Left" Margin="147,145,0,0" VerticalAlignment="Top" RenderTransformOrigin="-4.065,0.891" Width="89"/>
<Button x:Name="B_Start" Content="Start" HorizontalAlignment="Left" Margin="93,0,0,0" VerticalAlignment="Center" Height="35" Width="180" Click="button_Click"/>
<Button x:Name="B_zwz" Content="Mark" HorizontalAlignment="Left" Margin="93,267,0,0" VerticalAlignment="Top" Height="35" Width="73" Click="B_zwz_Click"/>
<Button x:Name="B_zurueck" Content="Reset" HorizontalAlignment="Left" Margin="200,267,0,0" VerticalAlignment="Top" Height="35" Width="73" Click="B_zurueck_Click"/>
<ListBox x:Name="ListBox1" Margin="400,108,146,108"/>
</Grid>
</Window>