WinForm Session

Couldn't think of a better title for this but here goes:

Wanted something similar to the Web Session for use in WinForm apps but didn't want to go overboard.  Hopefully this is a thread safe implementation.  Not sure though:

use in your code like this:

      AppState appState = AppState.GetAppState();

      appState.Cache["test"] = "Hello";

 

      // or

 

      AppState.GetAppState().Cache["test"] = "Hello";

 

      // then later on...

 

      string data = AppState.GetAppState().Cache["test"].ToString();

Here is the code for the AppState:

 

using System;

using System.Collections;

 

namespace Development

{

  public class AppState

  {

    private static AppState _appState;

    private Hashtable _cache;

    private Hashtable _syncCache;

 

    private AppState()

    {

      _cache = new Hashtable();

      _syncCache = Hashtable.Synchronized( _cache );

    }

 

    public Hashtable Cache

    {

      get { return _syncCache; }

    }

 

    public static AppState GetAppState()

    {

      // Support multithreaded applications through

      // "Double checked locking" pattern which avoids

      // locking every time the method is invoked

      if( _appState == null )

      {

        // Only one thread can obtain a mutex

        using( System.Threading.Mutex mutex = new System.Threading.Mutex() )

        {

          mutex.WaitOne();   

 

          if( _appState == null )

            _appState = new AppState();

 

          mutex.Close();

        }

      }

 

      return _appState;

    }

  }

}

posted @ Tuesday, February 01, 2005 12:17 PM


Print

Comments on this entry:

# re: WinForm Session

Left by Kevin Jachelski at 3/21/2005 6:32 PM

I believe you are both looking too deeply into this. There is a much simpler way to achieve a 'session' effect using the System.Runtime.Remoting.Messaging.CallContext. As a matter of fact, Session and HttpContext use this very class for the underlying storage. It is thread safe and can even be used across app domains. Using this method, you may place whatever you want in there...hash table (to emulate session) or whatever else you require.

# re: WinForm Session

Left by Bill at 3/23/2005 6:37 AM

Kevin,
Thanks for the post. I should have known that there was an existing API to accomplish something like this.

-Bill

# re: WinForm Session

Left by Willie Tilton at 2/1/2005 12:57 PM

using System;
using System.Collections;

namespace SAAP.WizardPanels
{
public sealed class WizardState
{
private static WizardState _wizardState = null;
private Hashtable _cache;
private Hashtable _syncCache;

private WizardState()
{
_cache = new Hashtable();
_syncCache = Hashtable.Synchronized( _cache );
}

public Hashtable Values
{
get { return _syncCache; }
}

public static WizardState Cache
{
get
{
// Support multithreaded applications through
// "Double checked locking" pattern which avoids
// locking every time the method is invoked
if( _wizardState == null )
{
// Only one thread can obtain a mutex
using( System.Threading.Mutex mutex = new System.Threading.Mutex() )
{
mutex.WaitOne();

if( _wizardState == null )
_wizardState = new WizardState();

mutex.Close();
}
}
return _wizardState;
}
}
}
}

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 4 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011