早上写了一个线程,可以实现每隔1秒,在Label上动态更新数据的功能。具体代码如下:
 //定义线程
        private Thread threadProd;
        //private Thread threadGetProd;         //定义委托
        delegate void set_Text(string s);
        set_Text setText;        delegate void set_Time(string s);
        set_Time setTime;        //定义计数器
       // private static int Count = 0;        public ThreadForm()
        {
            InitializeComponent();
        }        private void ThreadForm_Load(object sender, EventArgs e)
        {
            //委托实例化
            setText = new set_Text(set_LabelText);
            setTime = new set_Time(set_LabelTime);
        }        private void set_LabelText(string s)//主程序调用的函数
        {
            lblCount.Text = s;//测试数据
        }        private void set_LabelTime(string s)
        {
            lblTime.Text = s;//测试时间
        }        public void run()
        {
            for (int i = 0; i < 86400; i++)
            {
                VerifyCode verifyCode = new VerifyCode();
                lblCount.Invoke(setText, new object[] { verifyCode.CreateVerifyCode() });
                lblTime.Invoke(setTime, new object[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") });
                Thread.Sleep(1000);
            }
        }        private void ThreadForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (threadProd.IsAlive)//判断线程是否存在
            {
                threadProd.Abort();//撤销线程
            }
        }        private void btnClick_Click(object sender, EventArgs e)
        {
            threadProd = new Thread(new ThreadStart(run));
            threadProd.Start();
        }
上面的线程就是不停的产生新数据,但是,我现在想再写一个线程 用于获取1分钟内Label变化的值。假如说1分钟60个,就是把这60个值显示出来,每分钟显示一次。

解决方案 »

  1.   

    定时的操作不要用Thread.Sleep,这不是正常的使用方式,就好像用西瓜刀去切猪肉一样,能不能切?能,但是很容易切到手哦。还有,如果不是必要,少开线程。不是说线程用的多就牛逼,而是合理。
      

  2.   

    一开始用的timer控件,马马虎虎可以实现功能,现在改成线程,遇见困难了,等待高手指点。
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.Diagnostics;
    using System.Threading;
    using System.Runtime.InteropServices;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        /// <summary>
            /// 锁啊锁
            /// </summary>
            private ReaderWriterLockSlim _RWLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);        // 打卡队列
            Queue<SigninInfo> _SigninQueue = new Queue<SigninInfo>(600);          /// <summary>
            /// 打卡签到进入队列
            /// </summary>
            /// <param name="obj"></param>
            private void Singin(Object obj)
            {
                while (true)
                {
                    _RWLock.EnterWriteLock();
                    try
                    {
                        SigninInfo entity = new SigninInfo();
                        entity.EmployeeID = System.Environment.TickCount;
                        entity.SigninTime = System.DateTime.Now;                    _SigninQueue.Enqueue(entity);
                    }
                    finally
                    {
                        _RWLock.ExitWriteLock();
                        System.Threading.Thread.Sleep(10);
                    }
                }
            }        /// <summary>
            /// 保存
            /// </summary>
            /// <param name="obj"></param>
            private void Save(Object obj)
            {
                while (true)
                {
                    _RWLock.EnterReadLock();
                    try
                    {
                        if (_SigninQueue.Count == 0)
                        {
                            continue;
                        }                    SigninInfo entity = _SigninQueue.Dequeue();                    System.Diagnostics.Debug.WriteLine(String.Format("员工ID={0},打卡时间={1}", entity.EmployeeID, entity.SigninTime));
                    }
                    finally
                    {
                        _RWLock.ExitReadLock();
                        System.Threading.Thread.Sleep(10);
                    }
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(Singin), null);
                ThreadPool.QueueUserWorkItem(new WaitCallback(Save), null);
            }
        }    /// <summary>
        /// 打卡信息
        /// </summary>
        public struct SigninInfo
        {
            public Int32 EmployeeID;
            public DateTime SigninTime;
        }
    }