隐藏

C# 多线程并发

发布:2020/12/22 16:43:27作者:管理员 来源:本站 浏览次数:1206

 public partial class MultiThread : Form
    {
 
 
        public int threadh;//线程代号 
        public string strUrl;//接收文件的URL 
        public FileStream fs;
        public HttpWebRequest request;
        public byte[] nbytes;//接收缓冲区 
        public int nreadsize;//接收字节数 
 
        int conts;
        int Conted;
        public MultiThread()
        {
            //不对错误线程进行调用
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        //ThreadStart start
        private void butquantity_Click(object sender, EventArgs e)
        {
            if (txtquantity.Text != "")
            {
                conts = Convert.ToInt32(txtquantity.Text);
 
                Thread[] threads = new Thread[conts];
                for (int i = 0; i < conts; i++)
                {
                    threads[i] = new Thread(new ThreadStart(ReadHttp));
                    threads[i].Name = i.ToString() + "线程号";
                }
 
                foreach (Thread t in threads)
                {
                    t.Start();
                    //textBox2.Text = Thread.CurrentThread.Name;
                }
                InitializeTimer();
            }
            else
            {
                MessageBox.Show("请求数量不能为空!", "ERROR");
            }
        }
 
        private void timerClock_Elapsed(object source, ElapsedEventArgs e)
        {
            if (txtquantity.Text != "")
            {
                conts = Convert.ToInt32(txtquantity.Text);
 
                Thread[] threads = new Thread[conts];
                for (int i = 0; i < conts; i++)
                {
                    threads[i] = new Thread(new ThreadStart(ReadHttp));
 
                    threads[i].Name = i.ToString() + "线程号";
                }
                foreach (Thread t in threads)
                {
                    t.Start();
                }
            }
            else
            {
                MessageBox.Show("请求数量不能为空!", "ERROR");
            }
        }
 
        //发送请求
        private void ReadHttp()
        {
            try
            {
                Conted = Convert.ToInt32(txtquantity.Text);
                System.Net.WebClient client = new System.Net.WebClient();
                strUrl = txtAddress.Text;
                //this.textBox2.Text = "正在请求" + strUrl;
                Stream st = client.OpenRead(strUrl);
                StreamReader sr = new StreamReader(st);
                string res = sr.ReadToEnd();
                if (res != null)
                {
                    listBox1.Items.Add("-->" + Thread.CurrentThread.Name + "成功");
                }
                else
                {
                    listBox1.Items.Add("-->" + Thread.CurrentThread.Name + "失败");
                }
                sr.Close();
                st.Close();
                Thread.Sleep(2000);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示:");
                //throw;
            }
        }
        public void InitializeTimer()
        {
            System.Timers.Timer timerClock = new System.Timers.Timer();
            timerClock.Elapsed += new ElapsedEventHandler(timerClock_Elapsed);
            //timerClock.Interval = 1000 * 60 * 3;
            timerClock.Interval = 3000;
            timerClock.Enabled = true;
 
 
        }
 
    }
}