毫秒计时器

来源:互联网 发布:建造师网络继续教育 编辑:程序博客网 时间:2024/05/05 22:55

namespace TextProjext
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public delegate void Timers();
        public Timers tis;
       public void Tian()
        {
          
            while (true)//永远执行Tigers()这个方法;
            {
                Tigers();
                Thread.Sleep(10);//沉睡1000毫秒(每隔1000毫秒执行一下)
            }
           
        }
        public void Tigers()
        {
            try
            {
            if (!this.textBox1.InvokeRequired)
            {
                this.textBox1.Text = DateTime.Now.Second.ToString();//如果为false执行
               
            }
            else
            {
                this.Invoke(new Timers(Tigers));//如果为true执行;
            }
            }
            catch (Exception)
            {
                new Thread(Tian).Abort();
            }
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //主线程开始
            new Thread(Tian).Start();
            this.textBox1.Text = DateTime.Now.Second.ToString();
        }

 

 

 

 

获取当前电脑cpu的使用率


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 ApplactionTexe
{
    public partial class CPUContest : Form
    {
        public CPUContest()
        {
            InitializeComponent();
        }
        Thread thread;//
        private void button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(new ThreadStart(HuoCpu));
            thread.Start();//开始线程的操作
            this.button1.Enabled = false;
        }
        /// <summary>
        ///
        /// </summary>
       public delegate void Cpus();//定义了一个委托的对象
        WorkManager workmanager = new WorkManager();
        private void HuoCpu()
        {
            while (true)
            {
                Thread.Sleep(1000);//线程沉睡1000毫秒
                ManagerCpus();
            }
        }
        /// <summary>
        /// 获取CPU的使用率
        /// </summary>
        private void ManagerCpus()
        {
            try
            {
                if (this.listBox1.InvokeRequired)
                {
                    Cpus cpus = new Cpus(ManagerCpus);
                    this.Invoke(cpus);
                }
                else
                {
                    this.listBox1.Items.Add("您目前CPU的使用情况是:"+workmanager.CpuLoad.ToString().Substring(0,1)+"%");
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                thread.Abort();
                this.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }
    }
}



public class WorkManager
    {
       private PerformanceCounter pcCpuLoad;
       public WorkManager()
       {
           pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
       }
       /// <summary>
       /// 获取cpu利用率
       /// </summary>
       public float CpuLoad
       {
           get
           {
               return pcCpuLoad.NextValue();
           }
       }

原创粉丝点击