在開辟了多個線程后,
突然需要使用Thread.Sleep()來阻塞5s某一線程,
這下迷糊了,使用Thread.Sleep()的結果到底影響那個線程呢?
請賜教!

解决方案 »

  1.   

    如果没记错的话,
    前缀是 System.Thread.
    //不知所以不明 
    Thread.Sleep();
      

  2.   

    可以打印出当前线程看一下
    //Namespace References
    using System.Threading;/// <summary>
    /// method for retrieving the name of the current thread
    /// </summary>
    /// <returns></returns>
    public string RetrieveCurrentThreadName()
    {
        //variable to hold the thread name
        string name;
        try
        {
            //get the current thread
            Thread current = Thread.CurrentThread;
            name = current.Name;
        }
        catch (ThreadStateException ex)
        {
            name = string.Empty;
            MessageBox.Show(ex.Message);
        }
        catch (Exception ex)
        {
            name = string.Empty;
            MessageBox.Show(ex.Message);
        }
        return name;
    }
      

  3.   

    确切的说 thread.sleep 是放弃了占用cpu,请求不在被分配时间直到你给定的5秒时间经过 ,时间是0的话其实是放弃了cpu的时间刚刚够其他在时间片队列里的活动线程被执行
      

  4.   

    http://blog.csdn.net/lying_20101021/article/details/6959195using 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 TEST
    {
        public delegate void _Delegate_T();     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        _Delegate_T _DT_T1 = null;
            _Delegate_T _DT_T2 = null;
            _Delegate_T _DT_T3 = null;
            Thread T1 = null;
            Thread T2 = null;
            Thread T3 = null;        private void button1_Click(object sender, EventArgs e)
            {
                T1 = new Thread(p1);
                T2 = new Thread(p2);
                T3 = new Thread(p3);            _DT_T1 = new _Delegate_T(_DT_T_M1);
                _DT_T2= new _Delegate_T(_DT_T_M2);
                _DT_T3 = new _Delegate_T(_DT_T_M3);            T1.IsBackground = true;
                T1.Start();            T2.IsBackground = true;
                T2.Start();            T3.IsBackground = true;
                T3.Start();
            }        public void _DT_T_M1()
            {
                if (progressBar1.InvokeRequired)
                {
                    progressBar1.Invoke(_DT_T1);
                }
                else
                {
                    progressBar1.Increment(1);
                    if (progressBar1.Value == progressBar1.Maximum)
                        progressBar1.Value = 0;
                }
            }
            public void _DT_T_M2()
            {
                if (progressBar2.InvokeRequired)
                {
                    progressBar2.Invoke(_DT_T2);
                }
                else
                {
                    progressBar2.Increment(1);
                    if (progressBar2.Value == progressBar2.Maximum)
                        progressBar2.Value = 0;
                }
            }
            public void _DT_T_M3()
            {
                if (progressBar3.InvokeRequired)
                {
                    progressBar3.Invoke(_DT_T3);
                }
                else
                {
                    progressBar3.Increment(1);
                    if (progressBar3.Value == progressBar3.Maximum)
                        progressBar3.Value = 0;
                }
            }        public void p1()
            {
                while (this.T1.IsAlive)
                {
                    Thread.Sleep(500);
                    _DT_T1();
                }
            }
            public void p2()
            {
                while (this.T2.IsAlive)
                {
                    Thread.Sleep(1000);
                    _DT_T2();
                }
            }
            public void p3()
            {
                while (this.T3.IsAlive)
                {
                    Thread.Sleep(2000);
                    _DT_T3();
                }
            }
        }
    }