ASP.NET:如何在线程方法中获取QueryString的值?

时间:2011-12-07 16:39:10

标签: asp.net

我在线程方法中尝试获取Int32.Parse(Request.QueryString["PLCID"].ToString())的值时遇到错误,我尝试使用参数化线程但是faild

这里是我的代码段

protected void btnReqConfirm_Click(object sender, EventArgs e)
{
    Int32 val = Int32.Parse(Request.QueryString["PLCID"].ToString());
    id = Guid.NewGuid();

    ThreadStart ts = new ThreadStart(longRunningProcess);
    Thread th = new Thread(ts);
    th.Start();

    Response.Redirect("Status.aspx?ID="+id.ToString());
}
protected void longRunningProcess()
{
    String _MachineName = String.Empty;
    _MachineIP = String.Empty;
    DBLayer.getMachineByPLCID(Int32.Parse(Request.QueryString["PLCID"].ToString()), out _MachineName, out _MachineIP);

    if (DBLayer.insertRequest(User.Identity.Name, DBLayer.getMachineID(_MachineName), Convert.ToDateTime(lblReqFromVal.Text), Convert.ToDateTime(lblReqToVal.Text)))
    {
        _PLCName = DBLayer._getPlCByID(Int32.Parse(Request.QueryString["PLCID"].ToString()));

        //stop mail now
        LogicLayer.AddNewListItem("Confirmation Mail", "You can initiate your session directly from this link : " + string.Format(@"http://IP/RRRProject/DirectSessionPage.aspx?Machine={0}", _MachineIP), (User.Identity.Name).Substring(5) + "@invensys.com");
        lblConfirmationMessage.Visible = true;
        lblConfirmationMessage.ForeColor = Color.Green;

        Thread.Sleep(9000);
        SimpleProcessCollections.Add(id, "Some result.");
    }
}

任何想法?

2 个答案:

答案 0 :(得分:1)

我认为新主题并没有与第一个HttpContext相关联; Request.QueryString来电是HttpContext.Current.Request.QueryString的来电,因此ts不会看到您的网络应用的参数。

无论如何,您可以将QueryString传递给Thread并使用它。查看代码示例。

protected void btnReqConfirm_Click(object sender, EventArgs e)
{
 ...
   ThreadStart ts = new ThreadStart(longRunningProcess(Request.QueryString));
 ...
}

protected void longRunningProcess(NameValueCollection queryString)
{
 ...

    _PLCName = DBLayer._getPlCByID(Int32.Parse(queryString["PLCID"].ToString()));
 ...
}

答案 1 :(得分:0)

您是否有理由不能将查询字符串或您需要的值的对象传递给您的函数?