Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,

I want to know if User is online or offline,
and i used timer to update User last visit in database every 5 seconds,

I am using System.Timers.Timer in ASP.net 2.0,

When I use it I notice that the elapse event is run in new thread as it does not cause a Postback event to fire, but the problem is that when I close the browser the elapse event is still being raised and not stopped.

I want to stop the timer when the browser is closed or when I navigate to another website, is there any approach to stop it?

also when i make Sign out and execute statement _timer.Stop() the Timer not stopped.

That is all code:

C#
public partial class MainMasterPage : System.Web.UI.MasterPage
{
    protected System.Timers.Timer _timer;

    protected void Page_Init(object sender, EventArgs e)
    {
        _timer = new System.Timers.Timer(5000);

        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            _timer.Start();
        }
        else
        {
            _timer.Stop();
        }
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // Code here for update User last visit in database
    }
}
Posted
Updated 13-Nov-11 23:26pm
v7
Comments
[no name] 14-Nov-11 4:04am    
What are you using the timer for and how are you creating it?
MrLonely_2 14-Nov-11 4:18am    
Thanks man, i updated my question.
[no name] 14-Nov-11 4:20am    
What are you trying to accomplish with this code? It makes no sense
MrLonely_2 14-Nov-11 4:23am    
I am asking about stopping my timer,
not important about the sense
[no name] 14-Nov-11 4:28am    
It makes no sense to even use a timer. Timers consume resources and the server will only be able to create so many. Your site will suffer poor performance.

If you want to time how long a user is logged in for then this is not the way to accomplish it.

Set a session variable with the time logged in then compare it when logged out. Of course you will also need to handle the case of user leaving without logging out.

Given that you want to know if a user is still on your site you could create a 1px blank image that sits somewhere on your page and use ajax to refresh the link every n seconds. If you append the session id of the client to the image url you can detect on the server that the user is still connected.
 
Share this answer
 
Comments
[no name] 14-Nov-11 5:50am    
So you have x number of clients polling the server every second? How do you think performance will be on the server? How will this tell you when a client is no longer connected?
MrLonely_2 14-Nov-11 5:56am    
Where are you man ?
i am waiting for you
[no name] 14-Nov-11 17:35pm    
I'm sorry if you got the impression I have nothing else to do but answer you.
MrLonely_2 15-Nov-11 0:28am    
If you have no time then Why involve yourself with my question from the beginning,
It is either you do not know the answer or you do not want to tell me it,
Please, do not involve yourself with my questions again.
Use javascript timeout to call webservice, then do job you want.
 
Share this answer
 
Comments
Richard MacCutchan 31-Dec-13 7:57am    
This question is two years old; I think your answer may be too little too late.
Dave Kreskowiak 31-Dec-13 10:26am    
Wow. I just got bit by it too. I hate that the view counts have been removed from the question lists. At least then you had a rather large visual clue that the question was old.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900