Solved

Autologoff from C# app

Posted on 2010-08-21
6
641 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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