Link to home
Start Free TrialLog in
Avatar of wjstarck
wjstarck

asked on

Autologoff from C# app

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
ASKER CERTIFIED SOLUTION
Avatar of ksrsrinivasan
ksrsrinivasan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tried >me.close?
Avatar of wjstarck
wjstarck

ASKER

#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)
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

nvm ksrsrinivasan, that works just fine.

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

Thanks,