专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C# 多线程并发

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:43:31       共计:3639 浏览

 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#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法 | ·下一条:C#操作MongoDB

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有