多用户模拟访问站点


        public void MainStart()
        {
            if (server.Equals("") || intusernum == 0)
            {
                return;
            }

            //开始创建子线程
            for (int i = 0; i < intusernum; i++)
            {
                Thread Users = null;
                try
                {
                    Users = new Thread(new ThreadStart(this.ceshi));
                    Users.Priority = ThreadPriority.Normal;
                    Users.Start();
                }
                catch (Exception e)
                {
                    this.richTextBox1.Text += "发生错误:" + e + "/r/n";
                    isend = true;
                    this.button1.Enabled = true;
                    this.button2.Enabled = false;
                    return;
                }
            }
        }

        //子线程:检测得到页面时间
        public void ceshi()
        {
            //MessageBox.Show("线程开始", "警告");
            while (!isend)
            {
                Random rdm1 = new Random(unchecked((int)DateTime.Now.Ticks));
                string tname = rdm1.Next().ToString();

                try
                {
                    DateTime temptime = new DateTime();
                    temptime = DateTime.Now;

                    WebRequest myRequest = WebRequest.Create(server);        //出事化WEB页面
                    WebResponse myResponse = myRequest.GetResponse();        //返回RESPONSE对象

                    Stream stream = myResponse.GetResponseStream();
                    StreamReader sr = new StreamReader(stream);

                    string retstr = "";
                    string tmp = "";
                    while ((tmp = sr.ReadLine()) != null)
                    {
                        retstr += tmp;
                    }
                    myResponse.Close();        //管理RESPONSE对象

                    this.Invoke(new MethodInvoker(
                        delegate
                        {
                            this.richTextBox1.Text += "线程" + tname + "执行时间为:" + DateTime.Now.Subtract(temptime) + "豪秒/r/n/r/n";
                        }
                        ));

                }
                catch (Exception ex)
                {

                    this.Invoke(new MethodInvoker(
                    delegate
                    {
                        this.richTextBox1.Text += "错误:您输入的URL地址不正确,或该页面不存在!" + ex.Message + "/r/n/r/n";
                    }));
                }


            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值