Solved

Autologoff from C# app

Posted on 2010-08-21
6
643 Views
Last Modified: 2013-11-08
Hello-

I want to adapt the code here to present my program's logon dialog box after a period of inactivity.

This works, except that instead of Application.Exit(), I want to present my Logon form. I can't figure out how to kill the EventHandler from firing once my form loads.

ActivityMonitor.ActivityMonitor _am = new ActivityMonitor.ActivityMonitor();
_am.WarningMinutes = 0.9;
_am.MaxMinutesIdle = 1;
_am.Idle += new EventHandler(am_Idle); 

void am_Idle(object sender, EventArgs e)
{
    Application.Exit();
} 

Open in new window


If I place anything other than Application.Exit() in this location, mulitple instances of my form are created, no matter what I do to try to dispose of the EventHandler.

Any ideas/suggestions are welcome
0
Comment
Question by:wjstarck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
ksrsrinivasan earned 500 total points
ID: 33492730
One solution would be to disable the activity monitor and remove the event handler and display the login form.
Second one would be to start a new instance of the application and close the current one.
Option 1:

void am_Idle(object sender, EventArgs e)
{
    _am.Enabled = false; //Assuming that a similar property exists
    _am.Idle -= new EventHandler(am_Idle); 
    //logic to show your login form.
}

Option 2:
void am_Idle(object sender, EventArgs e)
{
    //Start a new instance of the application
    System.Diagnostics.Process.Start(Application.ExecutablePath);
    //Close the current instance
    Application.Exit();
}

Open in new window

0
 
LVL 12

Expert Comment

by:NormanMaina
ID: 33492761
tried >me.close?
0
 
LVL 12

Expert Comment

by:NormanMaina
ID: 33492779
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:wjstarck
ID: 33492815
#1 - there's no enable property
#2 - oddly, I get the same result (the application quits, but then multiple instances are spawned when it reopens)
0
 
LVL 6

Expert Comment

by:ksrsrinivasan
ID: 33492906
try one of these options.
Option 1:
void am_Idle(object sender, EventArgs e)
{
    _am.Idle -= new EventHandler(am_Idle); 
    //Start a new instance of the application
    System.Diagnostics.Process.Start(Application.ExecutablePath);
    //Close the current instance
    Application.Exit();
}

Option 2:

bool blnRestarting = false;

void am_Idle(object sender, EventArgs e)
{
   if (blnRestarting)
       return;
   blnRestarting = true;
    //Start a new instance of the application
    System.Diagnostics.Process.Start(Application.ExecutablePath);
    //Close the current instance
    Application.Exit();
}

Open in new window

0
 

Author Closing Comment

by:wjstarck
ID: 33492930
nvm ksrsrinivasan, that works just fine.

Accidentally had moved the instantiation of the EventHandler inside a foreach loop. Ouch.

Thanks,
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# ftp code 3 56
Need to find a web event on the IIS server 4 54
SQL State HYT00. Timeout expired proplem 8 44
ASP.NET VB code for check and insert in a table 4 45
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question