码迷,mamicode.com
首页 > 编程语言 > 详细

C# 多线程并发

时间:2014-10-29 12:31:25      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

bubuko.com,布布扣




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

C# 多线程并发

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/andyhuo/p/4058912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!