Solved

Autologoff from C# app

Posted on 2010-08-21
6
639 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
ksrsrinivasan earned 500 total points
Comment Utility
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
Comment Utility
tried >me.close?
0
 
LVL 12

Expert Comment

by:NormanMaina
Comment Utility
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:wjstarck
Comment Utility
#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
Comment Utility
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
Comment Utility
nvm ksrsrinivasan, that works just fine.

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

Thanks,
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now