在 WPF 中创建秒表 - 时间精度
Creating stopwatch in WPF - time precison
我需要在我的应用程序中设置秒表。我尝试使用调度程序计时器来做到这一点,间隔为一毫秒。但是,时钟的运行速度大约需要十秒才能数到一秒。
估计是线程问题,请问如何解决?
这个例子对你很有用。
public partial class MainWindow: Window
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
Stopwatch stopWatch= new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dispatcherTimer .Tick += new EventHandler(dt_Tick);
dispatcherTimer .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}",
ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
clocktxt.Text = currentTime;
}
}
private void startbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dispatcherTimer .Start();
}
private void stopbtn_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
{
stopWatch.Stop();
}
elapsedtimeitem.Items.Add(currentTime);
}
private void resetbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Reset();
clocktxt.Text = "00:00:00";
}
}
完整代码。
前端XAML如下:
<Window x:Class="StopWatch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Stop Watch" Height="350" Width="525">
<Grid Background="BlanchedAlmond">
<TextBlock FontSize="50" Margin="200,-12,175,258" RenderTransformOrigin="1.443,0.195">Timer</TextBlock>
<TextBlock x:Name="clocktxtblock" FontSize="70" Margin="118,38,37,183"></TextBlock>
<Button x:Name="startbtn" Margin="38,137,350,126" Background="SkyBlue" Content="Start" FontSize="30" Click="startbtn_Click" ></Button>
<Button x:Name="stopbtn" Margin="200,137,190,126" Background="SkyBlue" Content="Stop" FontSize="30" Click="stopbtn_Click" ></Button>
<Button x:Name="resetbtn" Margin="360,137,28,126" Background="SkyBlue" Content="Reset" FontSize="30" Click="resetbtn_Click" ></Button>
<ListBox x:Name="elapsedtimeitem" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="433" Margin="56,199,0,0"/>
</Grid>
编码代码隐藏文件(MainWindows.xaml.cs):
public partial class MainWindow: Window
{
DispatcherTimer dt = new DispatcherTimer();
Stopwatch sw = new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
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);
clocktxtblock.Text = currentTime;
}
}
private void startbtn_Click(object sender, RoutedEventArgs e)
{
sw.Start();
dt.Start();
}
private void stopbtn_Click(object sender, RoutedEventArgs e)
{
if (sw.IsRunning)
{
sw.Stop();
}
elapsedtimeitem.Items.Add(currentTime);
}
private void resetbtn_Click(object sender, RoutedEventArgs e)
{
sw.Reset();
clocktxtblock.Text = "00:00:00";
}
}
我需要在我的应用程序中设置秒表。我尝试使用调度程序计时器来做到这一点,间隔为一毫秒。但是,时钟的运行速度大约需要十秒才能数到一秒。
估计是线程问题,请问如何解决?
这个例子对你很有用。
public partial class MainWindow: Window
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
Stopwatch stopWatch= new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dispatcherTimer .Tick += new EventHandler(dt_Tick);
dispatcherTimer .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}",
ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
clocktxt.Text = currentTime;
}
}
private void startbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dispatcherTimer .Start();
}
private void stopbtn_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
{
stopWatch.Stop();
}
elapsedtimeitem.Items.Add(currentTime);
}
private void resetbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Reset();
clocktxt.Text = "00:00:00";
}
}
完整代码。
前端XAML如下:
<Window x:Class="StopWatch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Stop Watch" Height="350" Width="525">
<Grid Background="BlanchedAlmond">
<TextBlock FontSize="50" Margin="200,-12,175,258" RenderTransformOrigin="1.443,0.195">Timer</TextBlock>
<TextBlock x:Name="clocktxtblock" FontSize="70" Margin="118,38,37,183"></TextBlock>
<Button x:Name="startbtn" Margin="38,137,350,126" Background="SkyBlue" Content="Start" FontSize="30" Click="startbtn_Click" ></Button>
<Button x:Name="stopbtn" Margin="200,137,190,126" Background="SkyBlue" Content="Stop" FontSize="30" Click="stopbtn_Click" ></Button>
<Button x:Name="resetbtn" Margin="360,137,28,126" Background="SkyBlue" Content="Reset" FontSize="30" Click="resetbtn_Click" ></Button>
<ListBox x:Name="elapsedtimeitem" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="433" Margin="56,199,0,0"/>
</Grid>
编码代码隐藏文件(MainWindows.xaml.cs):
public partial class MainWindow: Window
{
DispatcherTimer dt = new DispatcherTimer();
Stopwatch sw = new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
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);
clocktxtblock.Text = currentTime;
}
}
private void startbtn_Click(object sender, RoutedEventArgs e)
{
sw.Start();
dt.Start();
}
private void stopbtn_Click(object sender, RoutedEventArgs e)
{
if (sw.IsRunning)
{
sw.Stop();
}
elapsedtimeitem.Items.Add(currentTime);
}
private void resetbtn_Click(object sender, RoutedEventArgs e)
{
sw.Reset();
clocktxtblock.Text = "00:00:00";
}
}