当时写这个程序是公司要求有个测试环境测试多用户情况,我匆匆忙忙的用了个上午写完后,一测试能用,就交差了,不过后来在多次使用的时候发现了有时候程序回莫名退出,不过由于时间等原应,就没有在仔细修改了,朋友门如果有新区的话,可以自己修改一下,如果有什么想法也希望和我联系!
功能说明:可以输入需要访问的url(注意需要加"http://"才可以,我当时程序中偷懒了,和想象中的模拟用户数,然后确定就可以了,如果你的输入都正确的话,程序会自动创建你希望的线程数目,然后一直运行,知道你电极了“停止”按扭,线程才会结束。在执行期间,线程会将访问一个网页所用到的时间,显示到文本框中,方便用户统计。
不多说了,看程序吧
using system;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.threading; //线程类
using system.collections ;
using system.net;
using system.io;
using system.xml;
using system.drawing;
using system.drawing.text;
namespace windowsapplication1
{
/// <summary>
/// summary description for form1.
/// </summary>
public class frmmain : system.windows.forms.form
{
private system.windows.forms.richtextbox richtextbox1;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.button button1;
private system.windows.forms.textbox url;
private system.windows.forms.textbox usernum;
private system.windows.forms.button button2;
private system.componentmodel.icontainer components;
private static string server; //测试页面地址
private static int intusernum; //虚拟测试人数
private system.windows.forms.timer timer1;
private static bool isend ; //定义是否中断程序(线程)运行
public frmmain()
{
server="";
intusernum=0;
isend=true;
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
private void initializecomponent()
{
this.components = new system.componentmodel.container();
this.usernum = new system.windows.forms.textbox();
this.richtextbox1 = new system.windows.forms.richtextbox();
this.url = new system.windows.forms.textbox();
this.label1 = new system.windows.forms.label();
this.timer1 = new system.windows.forms.timer(this.components);
this.label2 = new system.windows.forms.label();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.suspendlayout();
//
// usernum
//
this.usernum.location = new system.drawing.point(304, 320);
this.usernum.name = "usernum";
this.usernum.size = new system.drawing.size(48, 21);
this.usernum.tabindex = 5;
this.usernum.text = "";
//
// toolbarbutton1
//
//this.toolbarbutton1.imageindex = 0;
//
// richtextbox1
//
this.richtextbox1.location = new system.drawing.point(8, 40);
this.richtextbox1.name = "richtextbox1";
this.richtextbox1.size = new system.drawing.size(496, 272);
this.richtextbox1.tabindex = 1;
this.richtextbox1.text = "";
//
// url
//
this.url.location = new system.drawing.point(48, 320);
this.url.name = "url";
this.url.size = new system.drawing.size(168, 21);
this.url.tabindex = 2;
this.url.text = "";
//
// label1
//
this.label1.location = new system.drawing.point(8, 328);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(32, 16);
this.label1.tabindex = 3;
this.label1.text = "url:";
this.timer1.interval = 1000;
//
// label2
//
this.label2.location = new system.drawing.point(232, 328);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(64, 16);
this.label2.tabindex = 4;
this.label2.text = "虚拟人数:";
//
// button1
//
this.button1.location = new system.drawing.point(368, 320);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(64, 24);
this.button1.tabindex = 6;
this.button1.text = "确定";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.location = new system.drawing.point(440, 319);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(64, 25);
this.button2.tabindex = 7;
this.button2.text = "中断";
this.button2.click += new system.eventhandler(this.button2_click);
//
// frmmain
//
this.acceptbutton = this.button1;
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(512, 349);
this.controls.addrange(new system.windows.forms.control[] {
//this.toolbar1,
this.button2,
this.button1,
this.usernum,
this.label2,
this.label1,
this.url,
this.richtextbox1});
this.maximizebox = false;
this.name = "frmmain";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "站点测试";
this.load += new system.eventhandler(this.form1_load);
this.resumelayout(false);
}
#endregion
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
try
{
frmmain mainform = new frmmain();
mainform.menu =createmainmenu();
mainform.backcolor = color.darkgoldenrod ;
application.run(mainform);
}
catch(exception e)
{
messagebox.show("error:" + e, "警告");
return;
}
}
//创建工具条
public static mainmenu createmainmenu()
{
mainmenu my= new mainmenu();
menuitem menuitem1=new menuitem();
menuitem menuitem2=new menuitem();
menuitem menuitem3=new menuitem();
menuitem menuitem4=new menuitem();
menuitem1.text = "设置";
menuitem2.text = "帮助";
menuitem3.text = "帮助";
menuitem4.text = "版权";
menuitem2.menuitems.add(menuitem3);
menuitem2.menuitems.add(menuitem4);
eventhandler handle=new eventhandler(selectmenuitem) ;
menuitem1.click +=handle;
menuitem2.click +=handle;
menuitem3.click +=handle;
menuitem4.click +=handle;
my.menuitems.add (menuitem1);
my.menuitems.add(menuitem2);
return(my);
}
//点击工具条按纽后执行功能
public static void selectmenuitem(object sender, eventargs e)
{
messagebox.show(e.tostring() );
}
private void form1_load(object sender, system.eventargs e)
{
this.button1.enabled=true;
this.button2.enabled=false;
}
private void button1_click(object sender, system.eventargs e)
{
isend=false;
this.richtextbox1.text="";
this.button1.enabled=false;
this.button2.enabled=true;
if (this.url.text .equals(""))
{
messagebox.show("请输入您要测试的url地址!", "警告");
this.url.focus();
isend =true;
this.button1.enabled=true;
this.button2.enabled=false;
return;
}
else
{
server=url.text;
}
try
{
if (this.usernum.text.equals(""))
{
messagebox.show("请输入您希望的虚拟测试人数!", "警告");
isend =true;
this.button1.enabled=true;
this.button2.enabled=false;
this.usernum.focus();
return;
}
else
{
intusernum=int.parse(this.usernum.text );
}
}
catch(exception )
{
messagebox.show("您输入的测试人数不合法,请重新输入!", "警告");
this.usernum.text="";
isend =true;
this.button1.enabled=true;
this.button2.enabled=false;
this.usernum.focus();
return;
}
//开始启动线程
mainstart();
}
private void button2_click(object sender, system.eventargs e)
{
isend =true;
this.button1.enabled=true;
this.button2.enabled=false;
}
/////////////////////////////////////////////////////////////////////////////////////////
//下面为线程部分
/////////////////////////////////////////////////////////////////////////////////////////
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.richtextbox1.text += "线程" + tname + "执行时间为:" + datetime.now.subtract(temptime) + "豪秒\r\n\r\n";
}
catch(exception)
{
this.richtextbox1.text+="错误:您输入的url地址不正确,或该页面不存在!" + "\r\n\r\n";
break;
}
}
}
}
}
