C#-确定程序运行了多长时间

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DisplayRunTime
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private DateTime G_DateTime;//声明时间字段

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            G_DateTime = DateTime.Now;//得到系统当前时间
            Thread P_th = new Thread(//创建线程
                () =>//使用Lambda表达式
                {
                    while (true)//无限循环
                    {
                        TimeSpan P_TimeSpan =//得到时间差
                            DateTime.Now - G_DateTime;
                        Invoke(//调用窗体线程
                            (MethodInvoker)(() =>//使用Lambda表达式
                            {
                                tssLabel_Time.Text =//显示程序启动时间
                                    string.Format(
                                    "系统已经运行: {0}天{1}小时{2}分{3}秒",
                                    P_TimeSpan.Days, P_TimeSpan.Hours, 
                                    P_TimeSpan.Minutes, P_TimeSpan.Seconds);
                            }));
                        Thread.Sleep(1000);//线程挂起1秒钟
                    }
                });
            P_th.IsBackground = true;//设置为后台线程
            P_th.Start();//开始执行线程
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值