Static variables in an Async Method

Answered
0
0

Hi guys.

I have implemented Luca’s advice for Session Statics in our solution, which works great. ( https://wisej.com/support/question/shared-variables-and-methods )

However, I have noticed that if I make reference to any of its static variables from inside an Async Method, the StaticsInstance for the referenced “key” returns “null” although it had been already initialized. This way all previously set variables return null and after the Async runs, they are all null for the entire application.

Does this make sense or am I doing a wrong use of this technique?

Thanks in advance.

Ivan
(Wisej 2.2.44.0 – C# – SQLServer)

  • Ivan Borges
    Sorry, some more details: the Async Method ( a Socket connection OnConnect(IAsyncResult) ) runs from inside an “Application.StartTask()” method.
  • You must to post comments
Good Answer
0
0

The ThreadBegin and ThreadEnd events are not fired by regular threads (it’s a wisej event fired by wisej threads). Regular threads don’t fire any event. Set StaticsInstance = null before accessing so it’s restored from the session. Or use the Application.Session directly.

  • Ivan Borges
    I have set StaticsInstance to null from inside the Async method and noticed the Wisej.Web.Application.Session.key = StaticsInstance was set again, but full of null values. :-) Guess I will have to use the Application.Session directly.
  • You must to post comments
0
0

This is my “Statics”:


    public static class OSEStaticsHelper
    {
		static OSEStaticsHelper()
		{
			Wisej.Web.Application.ThreadBegin += OSEWjStaticsHelper_ThreadCleanUp;
			Wisej.Web.Application.ThreadEnd += OSEWjStaticsHelper_ThreadCleanUp;
		}

		[ThreadStatic]
		private static StaticsWrapper StaticsInstance;

		private static void OSEWjStaticsHelper_ThreadCleanUp(object sender, EventArgs e)
		{
			StaticsInstance = null;
		}

		public static StaticsWrapper Statics
		{
			get
			{
				if (StaticsInstance == null)
				{
					string key = typeof(StaticsWrapper).FullName;
					StaticsInstance = Wisej.Web.Application.Session.key;
					if (StaticsInstance == null)
					{
						StaticsInstance = new StaticsWrapper();
						Wisej.Web.Application.Session.key = StaticsInstance;
					}
				}
				return StaticsInstance;
			}
		}

		public class StaticsWrapper
		{
			public string ApplicationKey;
			...
		}
	}
  • Ivan Borges
    RunInContext is used in the MainPage. Where should I check the Application.SessionId ?
  • You must to post comments
0
0

Hi Luca.

Thanks for the reply!

Unfortunately it didn’t appear to make a difference, even using RunInContext all static values are still returning “null” inside the ASync method. Maybe I will have to change my Socket connection workflow.

  • Luca (ITG)
    Send me a code snippet of the “statics”. The context must be correct in RunInContext(). Check Application.SessionId.
  • You must to post comments
0
0

Hi Ivan, the socket callback is coming from a different thread and it’s out of context. The task in Application.StartTask() preserves the context because Wisej installs its synchronization manager, but a socket event is a different thread. Try this:

Application.RunInContext(this, () => { your code });

or, if you also need to update the browser:

Application.Update(this, () => { your code });

 

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.