logo

将当前请求的上下文作为参数传递给多线程的方法

作者:孟宪会 阅读:1512 发表于:2011-02-14 15:51:10

由于请求执行完毕后,多线程的程序可能还在运行,因此,在多线程的代码中,是无法直接使用 HttpContext 对象的,但是,我们可以把它作为参数及传递到多线程的代码中。实现这种功能,可以采用下面的两种方法:
一,将 HttpContext 对象作为类的成员传递,下面是这种方法的完整实例代码,直接拷贝即可执行:

ASPX 代码
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  public class ThreadWork
  {
    public System.Web.HttpContext ctx { set; get; }
    public ThreadWork() { }
    public
void DoTest()
    {
      
if (System.IO.File.Exists(HttpRuntime.AppDomainAppPath + "Log.txt"))
      {
        System.IO.File.Delete(HttpRuntime.AppDomainAppPath
+ "Log.txt");
      }

      
for (int i = 0; i < 10; i++)
      {
        
//将内容写入到文件里以记录测试的结果。
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(HttpRuntime.AppDomainAppPath + "Log.txt", true))
        {
          sw.WriteLine(
"当前时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 得到的参数:" + ctx.Request.QueryString["id"]);
          sw.Flush();
          sw.Close();
          sw.Dispose();
        }
        System.Threading.Thread.Sleep(
5000);
      }
    }
  }

  protected
void Page_Load(object sender, EventArgs e)
  {
    ThreadWork tw
= new ThreadWork();
    tw.ctx
= System.Web.HttpContext.Current;
    System.Threading.Thread thread
= new System.Threading.Thread(new System.Threading.ThreadStart(tw.DoTest));
    thread.Start();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  
<title></title>
</head>
<body>
  
<form id="form1" runat="server"></form>
</body>
</html>
我们可以以这种方法进行访问,
http://localhost:11249/WebSite1/ThreadTest.aspx?id=mengxianhui
得到的结果如下:
Txt 代码
当前时间:2011-02-11 20:11:07 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:12 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:17 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:22 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:27 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:32 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:37 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:42 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:47 得到的参数:mengxianhui
当前时间:2011-02-11 20:11:52 得到的参数:mengxianhui
二,采用 ParameterizedThreadStart 类实现,完整源代码如下:
ASpX 代码
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
protected void Page_Load(object sender, EventArgs e)
  {
    System.Threading.Thread thread
= new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(DoTest));
    thread.IsBackground
= true;
    thread.Start(System.Web.HttpContext.Current);
    Response.Write(
"页面请求完毕。");
  }

  
private static void DoTest(Object context)
  {
    System.Web.HttpContext ctx
= context as System.Web.HttpContext;
    
if (System.IO.File.Exists(HttpRuntime.AppDomainAppPath + "Log.txt"))
    {
      System.IO.File.Delete(HttpRuntime.AppDomainAppPath
+ "Log.txt");
    }
    
    
for (int i = 0; i < 10; i++)
    {
      
//将内容写入到文件里以记录测试的结果。
      using (System.IO.StreamWriter sw = new System.IO.StreamWriter(HttpRuntime.AppDomainAppPath + "Log.txt", true))
      {
        sw.WriteLine(
"当前时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 得到的参数:" + ctx.Request.QueryString["id"]);
        sw.Flush();
        sw.Close();
        sw.Dispose();
      }
      System.Threading.Thread.Sleep(
5000);
    }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
  
<form id="form1" runat="server"></form>
</body>
</html>
得到的结果与上面相同。