You are violating a major threading rule here. Your worker thread must not
touch objects owned by the main thread. The session object is owned by the
main thread. One solution is to modify your thread constructor to accept a
httpcontext object and then pass a reference to the current cache instance
to the thread. You can modify it safely in there. I've not checked to see if
this is the only buggy thing in the code by the way but this one caught my
eye.
The reason it is working in the first place is entirely dependent on the
underlying platform. It should fail consistenly on XP or server but work
sometimes on lesser platforms.
regards
--
-----------
Got TidBits?
Get it here:
www.networkip.net/tidbits
"Ilia" <anonymous@discussions.microsoft.com> wrote in message
news:0a5701c3a222$df3a17e0$a001280a@phx.gbl...[color=blue]
> Hi folks,
>
> I have some problems with ASP.NET Session State. The
> following simple program runs well if the Session State
> set as "InProc". If I switch to "SQLServer", the changes,
> made by the second thread, are lost. Any idea?
>
> I use VS.NET 2003 on Windows Server 2003 with hot fixes
> (as of 30-Oct-2003) and SQL Server 2000 SP 3a.
>
> Thanks.
> Ilia
>
> ---- WebForm1.aspx
> <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
> AutoEventWireup="false" Inherits="TestSqlState.WebForm1" %>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
> Transitional//EN" >
> <HTML>
> <HEAD>
> <title>WebForm1</title>
> <META HTTP-EQUIV="Refresh" CONTENT="3;
> URL=">
> <meta name="GENERATOR" Content="Microsoft
> Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name="vs_defaultClientScript"
> content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body>
> <form id="Form1" method="post"
> runat="server">
> <asp:Label id="lbl"
> runat="server">Label</asp:Label>
> </form>
> </body>
> </HTML>
>
>
> ---- WebForm1.cs
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
> using System.Threading;
> using System.Timers;
>
> namespace TestSqlState
> {
> /// <summary>
> /// Summary description for WebForm1.
> /// </summary>
> public class WebForm1 : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.Label
> lbl;
>
> private Thread m_Thread = null;
>
>
> private void Page_Load(object sender,
> System.EventArgs e)
> {
> lock(Session.SyncRoot)
> {
> if(Session["lbl"] == null)
> Session["lbl"]
> = "Start";
>
> lbl.Text = (string) Session
> ["lbl"];
> }
>
> m_Thread = new Thread(new
> ThreadStart(t1));
> m_Thread.Start();
>
> lock(Session.SyncRoot)
> {
> Session["lbl"] = (string)
> Session["lbl"] + "<br>Started...";
> }
> }
>
> private void t1()
> {
> System.Timers.Timer TimeOutTimer =
> new System.Timers.Timer(2000);
> ElapsedEventHandler
> TimeOutDelegate = new ElapsedEventHandler
> (TimeOutEventProcessor);
> TimeOutTimer.Elapsed +=
> TimeOutDelegate;
> TimeOutTimer.Enabled = true;
> }
>
> private void TimeOutEventProcessor(Object
> myObject, ElapsedEventArgs TimeOutArgs)
> {
> lock(Session.SyncRoot)
> {
> Session["lbl"] = (string)
> Session["lbl"] + "<br>Timer...";
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required
> by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support -
> do not modify
> /// the contents of this method with the
> code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.Load += new
> System.EventHandler(this.Page_Load);
>
> }
> #endregion
> }
> }
>[/color]