程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#多線程測試代碼

c#多線程測試代碼

編輯:C#入門知識

public class ThreadMulti
        {
 
            #region 變量
 
            public delegate void DelegateComplete();
            public delegate void DelegateWork(int taskindex, int threadindex);
 
            public DelegateComplete CompleteEvent;
            public DelegateWork WorkMethod;
 
            private Thread[] _threads;
            private bool[] _threadState;
            private int _taskCount = 0;
            private int _taskindex = 0;
            private int _threadCount = 20;//定義線程
          <span id="more-787"></span>
 
 
            #endregion
 
            public ThreadMulti(int taskcount)
            {
                _taskCount = taskcount;
            }
 
            public  ThreadMulti(int taskcount, int threadCount)
            {
                _taskCount = taskcount;
                _threadCount = threadCount;
            }
 
            #region 獲取任務 參考了老羽http://www.cnblogs.com/michael-zhangyu/archive/2009/07/16/1524737.html 的博客
            private int GetTask()
            {
                lock (this)
                {
                    if (_taskindex < _taskCount)
                    {
                        _taskindex++;
                        return _taskindex;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            #endregion
 
            #region Start
 
            /// <summary>
            /// 啟動
            /// </summary>
            public void Start()
            {
                //采用Hawker(http://www.cnblogs.com/tietaren/)的建議,精簡了很多
                _taskindex = 0;
                int num = _taskCount < _threadCount ? _taskCount : _threadCount;
                _threadState = new bool[num];
                _threads = new Thread[num];
 
                for (int n = 0; n < num; n++)
                {
                    _threadState[n] = false;
                    _threads[n] = new Thread(new ParameterizedThreadStart(Work));
                    _threads[n].Start(n);
                }
            }
 
            /// <summary>
            /// 結束線程
            /// </summary>
            public void Stop()
            {
                for (int i = 0; i < _threads.Length; i++)
                {
                    _threads[i].Abort();
                }
 
                //string s = "";
                //for (int j = 0; j < _threads.Length; j++)
                //{
                //    s += _threads[j].ThreadState.ToString() + "\r\n";
                //}
                //MessageBox.Show(s);
            }
            #endregion
 
            #region Work
 
            public void Work(object arg)
            {
                //提取任務並執行
                int threadindex = int.Parse(arg.ToString());
                int taskindex = GetTask();
 
                while (taskindex != 0 && WorkMethod != null)
                {
                    WorkMethod(taskindex, threadindex + 1);
                    taskindex = GetTask();
                }
                //所有的任務執行完畢
                _threadState[threadindex] = true;
 
                //處理並發 如果有兩個線程同時完成只允許一個觸發complete事件
                lock (this)
                {
                    for (int i = 0; i < _threadState.Length; i++)
                    {
                        if (_threadState[i] == false)
                        {
                            return;
                        }
                    }
                    //如果全部完成
                    if (CompleteEvent != null)
                    {
                        CompleteEvent();
                    }
 
                    //觸發complete事件後 重置線程狀態
                    //為了下個同時完成的線程不能通過上面的判斷
                    for (int j = 0; j < _threadState.Length; j++)
                    {
                        _threadState[j] = false;
                    }
                }
 
            }
 
            #endregion
        }
        private void button6_Click(object sender, EventArgs e)
        {
 
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadStartMethod2));
 
 
        }
        ThreadMulti thread;
        public void ThreadStartMethod2(object arg)
        {
            int workcount = listView1.Items.Count;
          ///  _count = workcount * 100;
 
            thread = new ThreadMulti(workcount);
 
            thread.WorkMethod = new ThreadMulti.DelegateWork(DoWork2);
            thread.CompleteEvent = new ThreadMulti.DelegateComplete(WorkComplete2);
            thread.Start();
        }
        public void DoWork2(int index, int threadindex)
        {
 
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(listView1.Items[index-1].SubItems[1].Text);
            MessageBox.Show(Convert.ToString(index));
            hwr.AllowAutoRedirect = false; //不允許重定向
            hwr.Timeout = Convert.ToInt32(numericUpDown3.Value)*1000; //連接超時時間設置
            hwr.Method = "GET"; //協議:GET、HEAD、POST、PUT、DELETE、TRACE 或OPTIONS。
            try
            {
                HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();
                button1.Text = ((int)hwrs.StatusCode).ToString();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
 
            }
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(threadindex * 5);
               // DisplayProgress2(threadindex, index, i);
            }
 
        }
        //結束
        public void WorkComplete2()
        {
            MessageBox.Show("檢測完畢");
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            thread.Stop();
        }
 
 
    }


摘自 http://www.xssxss.com/fuck/787.xss

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved