Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Autologoff from C# app

Posted on 2010-08-21
6
Medium Priority
?
646 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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