高手赐教B/S模式多线程例子,该如何处理
发布时间:2011-06-22 17:13:17 文章来源:www.iduyao.cn 采编人员:星星草
高手赐教B/S模式多线程例子
这是我的代码,但是感觉没有缩短时间
protected void Button3_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(new ThreadStart(bind2));
Thread thread1 = new Thread(new ThreadStart(bind1));
thread1.Start();
thread2.Start();
// thread.Join(TimeSpan.FromSeconds(100));
Thread.CurrentThread.Join(TimeSpan.FromSeconds(100));
thread2.Abort();
thread1.Abort();
}
protected void bind1()
{
string ids = this.txtID.Text.Trim(); //父类ID
string str = this.DropDownList2.SelectedValue.Trim();
if (str == "父级id值")
{
ids = this.txtID.Text.Trim();
}
else if (str == "class")
{
ids = "";
}
string xpath = this.txtXPath.Text.Trim();//抓取链接的xpath
string url = this.txtUrl.Text.Trim();//网站的url
string pagePath = "";
string s = getstr(url, ids, pagePath, xpath);//获取结果值
this.txtLianJie.Text += s;
}
protected void bind2()
{
string ids = this.txtID.Text.Trim(); //父类ID
string str = this.DropDownList2.SelectedValue.Trim();
if (str == "父级id值")
{
ids = this.txtID.Text.Trim();
}
else if (str == "class")
{
ids = "";
}
string xpath = this.txtXPath.Text.Trim();//抓取链接的xpath
string url = this.txtUrl.Text.Trim();//网站的url
string pagePath = "";
string s = getstr(url, ids, pagePath, xpath);//获取结果值
this.txtLianJie.Text += s;
}
------解决方案--------------------
顶:
创建了1个私有方法BindDataToRepeater用于把对应的SQL语句查询的结果绑定到对应的Repeater控件上。同时在此方法中使用了SqlDataReader,以提高绑定数据的速度。如果你使用了数据工厂可修改BindDataToRepeater中的具体实现过程;
2个共有方法BindCategory和BindArchive分别用于创建不同SQL语句、设置Repater的ID;
同时需要引入System.Web.UI、System.Web.UI.HtmlControls、System.Data.SqlClient3个必要的命名空间。
值得注意的是在BindDataToRepeater方法中使用了try..catch语句,但并没有在catch块中做任何事情,为什么我们用try.catch却不在catch块中做点什么事情呢,不是多此一举吗?使用try..catch是为了防止在执行BindDataToRepeater时抛出异常,若此处出现异常且此方法是在多线程中执行的,将会导致IIS进程崩溃,进而影响其他页面的正常执行,故而用try...catch防止BindDataToRepeater抛出错误。
我们之所以为数据绑定创建一个类,是为了提高内存利用率,当数据加载(Load)完毕的时候,为这个类创建的实例就会销毁。我们也可以通过在页面中创建几个全局变量来实现。但我还是建议以类的形式传递数据而不是使用全局变量。下面,我们开始在页面的Load中创建线程了。首先你需要在页面中引入System.Threading命名空间。
public classBindData
{
private int currentPage = 1;
private Repeater rpID;
public BindData(Repeater rpID)
{
this.rpID = rpID;
}
public BindData(Repeater rpID,int page)
{
this.rpID = rpID;
this.currentPage = page;
}
public void BindCategory()
{
string strSql="SELECT * FROM Category";
this.BindDataToRepeater(strSql, this.rpID);
}
public void BindArchive()
{
string strSql = string.Format("SELECT * FROM Archives WHERE Page={0}",this.currentPage);
this.BindDataToRepeater(strSql, this.rpID);
}
private void BindDataToRepeater(string strSql, Repeater rp)
{
if (rp == null) return;
SqlConnection conn = new SqlConnection("data source=数据服务器地址;User ID=用户名;pwd=密码;Initial Catalog=数据库名");
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader dtr;
try
{
conn.Open();
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
页面基类PageBase.cs代码,实现系统进入权限判断 这样思路有问题吗?
pagebase.cs 代码
C# code
public class PageBase:System.We...
-
linq中的表连接语句???
例如:album,artist,genre这三个表的连接语句。。谢谢
------解决方案--------------------
public PagedList<...
-
为什么Repeater控件生成的HTML有很多空行?
效果如下,每个超链接之后都自动换行。
<a target="_blank" href='http://v.gamefy.cn/v...
-
关于模式窗口关闭的问题(在线等,解答立刻结贴给分)
功能什么都做完了。问题有二:
1.关闭的时候父窗口不刷新。于是我在吊用模式窗口的...
-
ASP.NET 中为什么要把相对路径处理成 绝对路径
处理函数如下:
C# code
// Builds an absolute URL
private static strin...
-
asp.net里面像QQ相册那样列表然后预览的jquery特效
asp.net里面像QQ相册那样列表然后预览的jquery特效
谁有这样的完整例子,...
-
求高手共同破解JQGrid 4.1 for Asp.net(比GridView强大)
软件名称:jqSuite(此插件比asp.net自带GridView强大的多,但只能使用30天)
...
-
急!!!在线等 asp.net中页面传值刷新不跳转新建窗口
C# code
<asp:TemplateField HeaderText="操作管...
-
请教:asp.net 导出word文件思路
我要从数据库取数据,写到word文件里,请教高手指点.多谢了!
------解决方案--------------------...
-
jqGrid4定义按钮查询(用过jqGrid的高手请进,在线等)
如图,json格式数据已加载,如果不用JQGrid底部工具栏上查询按钮查询,用最上面自己...