0

I got an error when trying to get the value of Int32.Parse(Request.QueryString["PLCID"].ToString()) in the thread method and i tried to use parameterizedthread but faild

here my code snippet

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.");
    }
}

any idea??

1
  • Mind posting some details of this error that you speak of? Also multi-threading with asp.net doesn't work like it does with traditional applications. You cannot have a side-thread updating the controls after the main thread has already done Request.Response(). (maybe you want to look into AJAX?)
    – Bala R
    Dec 7, 2011 at 16:50

2 Answers 2

1

I suppose that the new Thread is not tied to the same HttpContext of the first one; a Request.QueryString call is a call to HttpContext.Current.Request.QueryString so ts won't see the parameters of your webapplications.

Anyway, you can pass the QueryString to the Thread and use it. Look at the code sample.

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()));
 ...
}
0

Is there a reason you can't just pass the querystring or an object of the values you'll need from it into your function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.