Solved

Autologoff from C# app

Posted on 2010-08-21
6
640 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 38
Authentication of Web Services 3 53
C# parent child form 5 19
WCF Service Application cannot connect from TCP terminal 1 14
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

13 Experts available now in Live!

Get 1:1 Help Now