C#多线程处理

Posted 行走江湖的码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#多线程处理相关的知识,希望对你有一定的参考价值。


        #region***变量初始化***
        string[] tx =  ;
        // 任务线程分派数 ,每次设定一组 ,一组十个线程

        // 任务线程 限制最多十个线程
        long threadCount = 0;
        long maxThreadCount = 10;
        ManualResetEventSlim manual = new ManualResetEventSlim(false);
        DataTable dt = new DataTable();
        Thread mainThread = null;
        int thSum = 0;
        //获取选择对象的结果
        string select_result = "";
        string webSite_ip = "";
        string RawTraffic = "";
        string TargetTraffic = "";

        //添加
        string[] webs =  ;
        ManualResetEvent manualEvent = new ManualResetEvent(true);
        DataRow[] drs;
        Commond com = new Commond();

        int gundong_num = 0;
        #endregion
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void yahoo_Load(object sender, EventArgs e)
        

            //使线程以非安全形式运行,允许在其它线程中调用主线程中界面控件
            ContainerControl.CheckForIllegalCrossThreadCalls = false;

        
        /// <summary>
        /// 开始查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        


                //获得选择结果
                webSite_ip = com.getSelectObject(select_result);
                try
                
                    mainThread = new Thread(() =>
                    
                        mainThreadMethod();
                    );
                    mainThread.Start();
                    mainThread.IsBackground = true;
                
                catch (Exception ex)
                
                    MessageBox.Show(ex.Message.ToString());
                
            

        /// <summary>
        /// 主线程:
        /// </summary>
        private void mainThreadMethod()
        

            try
            
                if (MessageBox.Show("抓取数据需要一点时间,是否继续?", "温馨提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                


                    // 界面、数据初始化
                    dt.Columns.Add("DateTime", typeof(string));
                    dt.Columns.Add("RawTraffic", typeof(string));
                    dt.Columns.Add("TargetTraffic", typeof(string));
                    dt.Columns.Add("WebSite", typeof(string));
                    DataRow dr = dt.NewRow();

                    //textBox1.Text = textBox1.Text.Replace(" ", "");

                    //tx = textBox1.Text.Trim().Split('\\n');

                    //progressBar1.Maximum = tx.Length;
                    //label4.Text = "0/" +tx.Length.ToString();
                    // 控制线程循环次数
                    foreach (var item in tx)
                    
                        Thread.Sleep(500);
                        try
                        
                            if (Interlocked.Read(ref this.threadCount) >= this.maxThreadCount)
                            
                                manual.Wait();
                                gundong_num++;
                                //MessageBox.Show((gundong_num++).ToString());
                            
                            Interlocked.Increment(ref this.threadCount);

                            ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
                            
                                try
                                
                                    Gethtml(obj);
                                
                                finally
                                
                                    //Interlocked.Decrement(ref this.threadCount);
                                    //this.manual.Set();
                                

                            ), item);
                        
                        catch (Exception)
                        

                            throw;
                        
                    

                

            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message.ToString());
            
        
        /// <summary>
        /// 方法:获取网页数据
        /// </summary>
        /// <param name="o"></param>
        public void GetHtml(object o)
        
        //LABLE1:

            try
            
                #region
                string webSite = o.ToString().Trim();
                string webSiteUrl = "http://" + webSite + webSite_ip;

                //gundong_num++;

                string strHtml = "";
                StreamReader sr = null;
                System.Text.Encoding code = System.Text.Encoding.UTF8;
                try
                
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webSiteUrl);
                    HttpWebResponse response = null;
                    request.KeepAlive = false;
                    request.Method = "GET";
                    // 设定超时时间为 30 秒钟
                    request.Timeout = 30000;
                    //if (request.Timeout > 30000) 
                    //    return;
                    //
                    //request.ServicePoint.Expect100Continue = false;
                    ServicePointManager.DefaultConnectionLimit = 100000;

                    using (response = (HttpWebResponse)request.GetResponse())
                    
                        if (response.StatusCode == HttpStatusCode.OK)
                        

                            sr = new StreamReader(response.GetResponseStream(), code);
                            strHtml = sr.ReadToEnd().Replace('\\t', ',');

                            sr.Close();
                        
                        else
                        
                            // 访问超时的网站 返回数据为-1
                            Thread.Sleep(1000);
                            strHtml = "-1,-1";
                        
                    

                
                catch
                

                    // 访问超时的网站 返回数据为-1
                    strHtml = "-1,-1";
                    //goto LABLE1;
                


                #endregion
                if (strHtml.IndexOf(',') < 0)
                


                
                // 数据存储
                //MessageBox.Show(strHtml);
                //if (comboBox1.SelectedIndex != 2)
                //
                //  strHtml=strHtml.Replace(",", "");
                //  DataRow dr = dt.NewRow();
                //  dr["DateTime"] = DateTime.Now.ToString();
                //  dr["RawTraffic"] = strHtml.ToString();
                //  dr["TargetTraffic"] = "";
                //  dr["WebSite"] = webSite;
                //  dt.Rows.Add(dr);
                //  progressBar1.Value++;

                //  label4.Text = progressBar1.Value + "/" + progressBar1.Maximum.ToString();
                //
                else
                

                    //MessageBox.Show("长度:" + strHtml.Length);
                    RawTraffic = strHtml.Substring(0, strHtml.IndexOf(',')+1);
                  string RawTraffic2=  RawTraffic.Replace(",", " ");
                    //MessageBox.Show(RawTraffic);
                  //MessageBox.Show(RawTraffic.Length.ToString());
                  //TargetTraffic = strHtml.Substring(strHtml.IndexOf(',') + 1, strHtml.Length - strHtml.IndexOf(',') - 1);
                  TargetTraffic = strHtml.Substring(RawTraffic.Length, strHtml.Length - RawTraffic.Length);
                  //MessageBox.Show(TargetTraffic);
                    DataRow dr1 = dt.NewRow();
                    dr1["DateTime"] = Convert.ToDateTime(DateTime.Now.ToString());
                    dr1["RawTraffic"] = RawTraffic2;
                    dr1["TargetTraffic"] = TargetTraffic;
                    dr1["WebSite"] = webSite;
                    dt.Rows.Add(dr1);
                    // 更新界面状态 控制线程数量 保持循环
                    //progressBar1.Value++;
                    ////progressBar1.Value = gundong_num-1 ;

                    //滚动进度条进度显示
                    //label4.Text = progressBar1.Value + "/" + thSum.ToString();
                    //label4.Text = progressBar1.Value + "/" + progressBar1.Maximum.ToString();
                

                    //label4.Text = gundong_num+ "/" + thSum.ToString();

                    //label4.Text = this.threadCount + "/" + thSum.ToString();
                    //threadCount--;
                    // 判断是否执行结束

            
            catch (Exception ex)
            
                //if (textBox2.Text == "")
                //
                //  textBox2.AppendText(ex.ToString());
                //
            
            finally
            
                Interlocked.Decrement(ref this.threadCount);
                this.manual.Set();

                //if (progressBar1.Value == progressBar1.Maximum && Interlocked.Read(ref this.threadCount) <= 0L)
                //
                //  label1.Text = "抓取结束!";
                //  label1.ForeColor = Color.Red;
                //  textBox1.Enabled = true;
                //  button1.Enabled = true;

                //  button3.Enabled = true;
                //  button4.Enabled = true;
                //  for (int i = 0; i < tx.Length; i++)
                //  
                //      //int j = 0;
                //      //j = dt.Rows.Count;
                //      //DataRow[] drs = dt.Select("WebSite  ='" + tx[i].ToString().Trim('\\r') + "'");

                //      drs = dt.Select("WebSite  ='" + tx[i].ToString().Trim() + "'");
                //      //textBox2.AppendText(drs[0][3].ToString() + "\\t" + drs[0][1].ToString() + "" + drs[0][2].ToString() + "\\r\\n");
                //      switch (select_result)
                //      
                //          case "0":
                //              
                //                  //google
                //                  getInfo1();
                //                  break;
                //              
                //          case "1":
                //              
                //                  //yahoo
                //                  getInfo1();
                //                  break;
                //              
                //          case "2":
                //              
                //                  textBox2.AppendText(drs[0][3].ToString() + "\\t" + drs[0][1].ToString() + "\\t" + drs[0][2].ToString() + "\\r\\n");
                //                  break;
                //              
                //      
                //  

                //  progressBar1.Value = progressBar1.Maximum;

                //  label4.Text = tx.Length + "/" + tx.Length;
                //  comboBox1.Enabled = true;

                
            
        

以上是关于C#多线程处理的主要内容,如果未能解决你的问题,请参考以下文章

C#多线程处理多个队列数据的方法

C#多线程如何处理数据

C#处理二进制文件,多线程处理

C#多线程处理

C#多线程开发-处理子线程中的异常

Winform c# 多线程处理实例 地址 可以下载!