Skip to main content

Statics

Static Variables​

Static variables in Wisej.NET are just regular C# static variables. They have the same value and are accessible across all client sessions. A static variable has one copy per AppDomain. So, if the AppDomain or AppPool resets, the value of the static variable resets.

You can create a static variable like so:

public static string myStaticVariable = "hello";

Static Events​

A static event in Wisej.NET is a standard C# static event. Each application instance on the server has one subscriber list, shared across browser sessions. Raising the event invokes every registered handler, including handlers subscribed by different sessions.

You can create a static event like so:

public static event EventHandler MyStaticEvent;

And attach a handler to the event like so:

MyStaticEvent += MyStaticEvent_Fired;

private void MyStaticEvent_Fired(object sender, EventArgs e)
{
//code that runs when the event is fired
}

You can fire the event like so:

MyStaticEvent?.Invoke(null, EventArgs.Empty);

A static event handler runs in the context of the code that raises the event, which may belong to another session or have no session context. To update the subscribing session's UI, use Application.Update with a component from that session:

private void MyStaticEvent_Fired(object sender, EventArgs e)
{
Application.Update(this, () =>
{
//example UI updates
button1.Enabled = true
button1.Text = "New Text";
label1.Text = "New Text";
});
}

Here, this is a component belonging to the subscribing browser session. Passing it to Application.Update lets Wisej.NET restore that session's context, run the supplied code, and send the UI changes to the correct browser.

info

If the code in the event handler doesn't update the UI, you can also use Application.RunInContext() instead of Application.Update() in order to avoid pushing a UI update to the browser.