Hi This Satani Ketan , My Question is i am Making A login Form using c# windows application..so i have to use session and coockies in my form..that provide me how much time spent by user after logout....can any one help me..
thanks in advance...!!!

Dani AI

Generated

Short summary and a practical pattern that follows advice from , expands on 's point that browser cookies/sessions are web concepts, and builds on 's suggestion to record timestamps.

A reliable approach for a WinForms app:

  • Use a Stopwatch for precise in-memory timing while the app runs (immune to clock changes).
  • Record UTC timestamps (LoginUtc and LogoutUtc) when persistence across restarts or audit is needed.
  • Persist one row per login session (UserName, LoginUtc, LogoutUtc, DurationSeconds) in a local DB or settings file.

Example implementation (keeps logic central so any form can call it):

using System;
using System.Diagnostics;

public class SessionTracker
{
    private DateTime? loginUtc;
    private readonly Stopwatch stopwatch = new Stopwatch();

    public void OnLogin()
    {
        loginUtc = DateTime.UtcNow;
        stopwatch.Restart();
    }

    public TimeSpan OnLogout()
    {
        stopwatch.Stop();
        var runtimeElapsed = stopwatch.Elapsed;
        var persistedElapsed = loginUtc.HasValue ? DateTime.UtcNow - loginUtc.Value : runtimeElapsed;
        // Persist loginUtc and DateTime.UtcNow (LogoutUtc) and persistedElapsed.TotalSeconds
        return persistedElapsed;
    }
}

Practical notes and troubleshooting:

  • Store timestamps in UTC to avoid DST and local-time issues. Convert to local only when displaying.
  • If the app may crash or be terminated, write periodic heartbeats (e.g., every minute) so partial time is not lost.
  • Guard against null loginUtc on logout and against multiple rapid logins/logouts.
  • For trustable timing in hostile environments, validate against a server time API instead of relying on the client clock.

This pattern is simple to implement, works for single or repeated sessions, and makes totals easy to compute and display (use TimeSpan.ToString(@"hh\:mm\:ss") for formatting).

Recommended Answers

All 2 Replies

I'm confused... you developing in Windows Forms with C#? Is that it?
If it is, then there's no cookies or sessions. You can just hold the login date on the main form of you app.

Why is this tagged with c,c++, java, pascal, python and vb.net? Please give correct information so people can help better.

commented: Good note about the tags. +6

If you are wanting to see how long somebody was logged into your applications, record the log-in time in a variable that will survive the life of your application (in a module or main form) and when the user logs out, use DateDiff to determine the time that the user was logged in. You can then display it using Date.Format or something similar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.