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

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 ?
 
C

charles

....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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top