Solved

Autologoff from C# app

Posted on 2010-08-21
6
645 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

626 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