基本信息
源码名称:C#多种计时方式的撰写和验证
源码大小:0.03M
文件格式:.7z
开发语言:C#
更新时间:2020-07-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍
本例将常用的几种计时方式都进行了尝试,并通过测试了解了各种计时的应用区别,方便后续的应用!代码里还有详细的解释说明,方便使用!

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.Diagnostics;//提供一组方法和属性,可用于准确地测量运行时间,只有引用后Stopwatch类才能使用
using System.Threading;//创建和控制线程,设置其优先级并获取其状态。

namespace 秒表计时器
{
    public partial class Form1 : Form
    {
        private BackgroundWorker demoBGWorker = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer1.Interval = 500;
        }

        int timerDrawI = 0;
        float timerDrawF = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timerDrawI ;
            timerDrawF = timerDrawI / 2;
            textBox_ms.Text = Convert.ToString(timerDrawI);
            textBox_sec.Text = Convert.ToString(timerDrawF);
            textBox_min.Text = Convert.ToString(timerDrawF / 60);
            textBox_hour.Text = Convert.ToString(timerDrawF / 3600);
            textBox_all.Text = GetTime(timerDrawF);

        }

        private void btn_Strat_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timerDrawI = 0;
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }


        string GetTime(float time)
        {
            double hour = Math.Floor(time / 3600f);//小时(即返回小于等于"time/3600f"的 最大整数)
            double min = Math.Floor(time / 60f - hour * 60f);// 分
            double sec = Math.Floor(time - min * 60f - hour * 3600f);// 秒
            return hour.ToString("00") ":" min.ToString("00") ":" sec.ToString("00");
        }

        private void Delay_time(int delay_in)
        {
            Stopwatch sw = new Stopwatch(); ;//定义Stopwatch 类将使用系统计数器来测量运行时间
            sw.Start();                      //系统计数器开始计时
            Thread.Sleep(delay_in);          //定义时间间隔
            sw.Stop();                       //结束计时
        }