I am not maintaining threads or session, however the variables defined are lost during a postback because a new thread is created. Thoughts?
currentthreadid and sessionid change every time when either button1 or button2 are clicked. I am more concerned about new threads being created. Help please !
here is my WebForm1.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UpdatePanelTest2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Before Click" />
<asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" CausesValidation="false"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click"/>
</form>
</body>
</html>
here is my WebForm1.aspx.cs code
namespace UpdatePanelTest2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string currentthreadid = AppDomain.GetCurrentThreadId().ToString();
string sessionid = Session.SessionID;
if (IsPostBack) { }
if (IsAsync) { }
if (IsPostBackEventControlRegistered) { }
if (ScriptManager1.IsInAsyncPostBack) { }
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "After Click";
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
}
my web.config file
<?xml version="1.0"?>
<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
UPDATE
The reason i need it to be the same thread for postback calls because i maintain certain variables and then re-use those variables for the current pool thread
code:
private static readonly ThreadLocal<PageContext> _context = new ThreadLocal<PageContext>(typeof(PageContext), "_context", true);
private string[] _path;
public static PageContext ForCurrentThread
{
get { return _context.Value ?? (_context.Value = new PageContext()); }
}
public void Initialize(HttpRequest _request)
{
somestring = null;
}
public string somestring { get; set; }
When the page loads the first time, somestring is given a value, other classes then use the same context and re-use that somestring value. This doesnt work if a new thread is created for postback requests.
This is by design. SessionID will not be saved until you actually use it.
https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx
When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed.
If you add this to your code the SessionID will remain the same:
Session["foo"] = 1;
ASP.Net will not guarantee you to get the same thread for each request. The application works with several threads and it will fetch a thread from the threadpool and use it to process the request.
I would say that it's nothing to worry about.
I would avoid storing stuff for the specific thread. There's no way of knowing if you're going to get the same thread on the next request. Shouldn't the value be stored for the specific user instead (eg. Session)?
public static PageContext ForCurrentSession
{
get
{
if(Session["PageContext"] == null)
{
Session["PageContext"] = new PageContext();
}
return Session["PageContext"] as PageContext;
}
}