我在线程方法中尝试获取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.");
}
}
任何想法?
答案 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)
您是否有理由不能将查询字符串或您需要的值的对象传递给您的函数?