C# 多线程并发




 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;         }     } }

转载于:https://www.cnblogs.com/andyhuo/p/4058912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值