在 WPF 中动态查看 Windows UpTime

发布于 2025-01-11 21:30:50 字数 481 浏览 4 评论 0原文

我想要获得如下图所示的 Windows UpTime

在此处输入图像描述

我正在使用 NetFrameWork 3 我使用此代码来显示 Windows 的 UpTime

PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
upTime.NextValue();
TimeSpan ts = TimeSpan.FromSeconds(upTime.NextValue());
UpTime.Text = "UpTime: " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds;

但我收到的内容是固定的并且不会自行更新 我想同时更改Windows的正常运行时间 请指导我

I want to get Windows UpTime like the picture below

enter image description here

I am using NetFrameWork 3
I use this code to display UpTime for Windows

PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
upTime.NextValue();
TimeSpan ts = TimeSpan.FromSeconds(upTime.NextValue());
UpTime.Text = "UpTime: " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds;

But what I receive is fixed and does not update itself
I want to change the uptime of Windows here at the same time
please guide me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2025-01-18 21:30:50

你的代码看起来不错。唯一缺少的是您应该定期调用它来更新 UI。

看一下示例: https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatchertimer?view=windowsdesktop-6.0

需要注意的是,因为您想要更新 UI:

为了访问用户界面 (UI) 线程上的对象,需要使用 Invoke 或 BeginInvoke 将操作发布到用户界面 (UI) 线程的 Dispatcher 上。使用 DispatcherTimer 而不是 System.Timers.Timer 的原因是 DispatcherTimer 与 Dispatcher 在同一线程上运行

您只需稍作修改的代码可能如下所示:

DispatcherTimer dispatcherTimer;

public MainWindow()
{
    InitializeComponent();
    DispatcherTimer_Tick(null, EventArgs.Empty);
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
    dispatcherTimer.Tick += DispatcherTimer_Tick;
    dispatcherTimer.Start();
}

private void DispatcherTimer_Tick(object? sender, EventArgs e)
{
    PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
    upTime.NextValue();
    TimeSpan ts = TimeSpan.FromSeconds(upTime.NextValue());
    UpTime.Text = "UpTime: " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds;
}

Your code looks good. The only thing missing is that you should call it periodically to update the UI.

Take a look at the example: https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatchertimer?view=windowsdesktop-6.0

Important to note, since you want to update UI:

In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer as opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher

Your only slightly modified code might then look something like this:

DispatcherTimer dispatcherTimer;

public MainWindow()
{
    InitializeComponent();
    DispatcherTimer_Tick(null, EventArgs.Empty);
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
    dispatcherTimer.Tick += DispatcherTimer_Tick;
    dispatcherTimer.Start();
}

private void DispatcherTimer_Tick(object? sender, EventArgs e)
{
    PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
    upTime.NextValue();
    TimeSpan ts = TimeSpan.FromSeconds(upTime.NextValue());
    UpTime.Text = "UpTime: " + ts.Days + ":" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文