I have created one project in windows application and I provided multiple users accessibility for the project but I need restrict it to only 3 users accessing it at a time. For that purpose I have added column status (bit), for every login I made status=true
and log out status=false
but if I ended my project from end task and system shutdown then I'm getting problem in counting logged users.
Is it possible to find active users and there status? If it is possible then I'll run trigger every 5 minutes and update the status fields accordingly.
OK, so since this is a Windows application, and you haven't provided any code, I'd do something like this when they login:
// connect to the database
using (SqlConnection c = new SqlConnection("connection string"))
{
// count the logged in users
var loggedInUsers = (int)new SqlCommand("SELECT COUNT(UserId) FROM Users WHERE status = 1", c).ExecuteScalar();
// if the threshold has been met then shut down the application
if (loggedInUsers == 3)
{
MessageBox.Show("There are already 3 concurrent users logged into the system -please try again at a later time.", "User Limit Met", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
}
but you also have to do this when the application shuts down, like in the OnClosing
method of the main form:
protected override void OnClosing(CancelEventArgs e)
{
// connect to the database
using (SqlConnection c = new SqlConnection("connection string"))
{
// log the user out
var cmd = new SqlCommand("UPDATE Users SET status = 0 WHERE UserId = @UserId", c);
cmd.Parameters.AddWithValue("@UserId", // get your user id from somewhere
var rowsAffected = cmd.ExecuteNonQuery();
// make sure the update worked
if (rowsAffected == 0)
{
// do something here to make sure they get logged out
}
}
}
To handle the edge case of an application failure during a task manager quit for example you should be able to leverage Application.Exit
, so in your startup method (ie where Application.Run
is called) put this line before that:
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
and then consume it:
private void Application_ApplicationExit(object sender, EventArgs e)
{
// and now you can probably just move all of the code here
// instead of the OnClosing
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.