How to send event to all sessions in ASP .NET ?

  • Thread starter Thread starter Borr
  • Start date Start date
B

Borr

Hi,

I am writing an ASP .NET application that has a following
logic : if one of application users performs some specific
action, ALL other users that are browsing some page (lets
call it "WaitingForSpecificAction.aspx"), should get event
that this action is performed.

How to do it ?

I defined class like this :

public class EventManager
{
public delegate void ActionPerformed(object source,
EventArgs e);
public event ActionPerformed OnActionPerformed;

public void PerformAction()
{
... // Fill EventArgs e variable
if (OnActionPerformed != null) {
OnActionPerformed(this, e);
}
}
}

This class instance is an entry in Application collection
and can be accessed as Application["EntryName"]. I call
PerformAction() method from a page where the user can
perform the action.

-------
On "WaitingForSpecificAction.aspx" I subscribe on
OnActionPerformed event :

EventManager em = Application["EntryName"] as EntryManager;
em.OnActionPerformed += new EventManager.ActionPerformed
(OnActionPerformed);

and define OnActionPerformed event handler :

private void OnActionPerformed (object source, GameEvent e)
{
...
}

It does not work ! For example, if OnActionPerformed()
method contains Response.Redirect or Server.Transfer
calls, it fails. Probably it is because of event is raised
in one session and handled in another one. But ... HOW TO
DO IT CORRECTLY ?
 
....But you can do a funky thing by polling an application variable every 10th of a second and setting the applcation variable...

Something Like:
public void WaitForAlert()
{
int iOldAlertVal = 0;
if (Application["TheAlert"] != null)
iOldAlertVal = Application["TheAlert"];
while (Application["TheAlert"] == iOldAlertVal)
System.Threading.Thread.Sleep(100);
}

public void SendAlert()
{
int iOldAlertVal = 0;
if (Application["TheAlert"] != null)
iOldAlertVal = Application["TheAlert"];
Application["TheAlert"] = iOldAlertVal + 1;
}

charles
 
Back
Top