System.Threading.Timer 使用

//定义计时器执行完成后的回调函数 
 TimerCallback timecallback = new TimerCallback(WriteMsg);

//定义计时器
            System.Threading.Timer t = new System.Threading.Timer(timecallback, "Hello Jack", 0, 1000);


//回调用执行函数
 private delegate void WriteMsgDelegate(object objData);
        private void WriteMsg(object objData)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new WriteMsgDelegate(WriteMsg), new object[] { objData });
            }
            else
            {
                int a, b;
                ThreadPool.GetAvailableThreads(out a, out b);
                string message = string.Format("{0}\n   CurrentThreadId is:{1}\n" +
                                                                        "   CurrentThread IsBackground:{2}\n" +
                                                                        "   WorkerThreads is:{3}\n   CompletionPortThreads is:{4}\n",
                                                                         objData + "Time now is " + DateTime.Now.ToLongTimeString(),
                                                                         Thread.CurrentThread.ManagedThreadId,
                                                                         Thread.CurrentThread.IsBackground.ToString(),
                                                                         a.ToString(), b.ToString());
                richTextBox1.AppendText(message + "\r\n");
            }
        }

 

posted @ 2017-01-09 14:25  奔跑.Ken  阅读(546)  评论(0编辑  收藏  举报