There are over one hundred od user controls. Most of them are loaded dynamically. Does your design work in this case?
Main Topics
Browse All TopicsAnyone can point the direction of implementing the time out function on windows platform (.NET, C#)?
There are multiple instances of the application I am working on. The user wants to implement a "Auto Save/Time out" function, so that the app will auto save the data (or popup the dialog box) if the instance is idled for certain period of time (say 30 minutes).
I need some solid direction of implementation. If there is any more issue come up, I will create a new thread of question.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In addition to mandalorian4's solution, you can create a function say
private void IsDirty( ) // -- some changes were made
{
Activity = true; // -- or update the flag that determines whether you should save or not
}
private void IsClean( ) // -- you can call this if your autosave is finished
{
Activity = false;
}
since function are more easily associated with events. Such that you can easily attach this function to events
in that way, even if controls are created dynamically, you could assign certain events of that control to the function
ie.
button.Click += new EventHandler(IsDirty);
textbox.KeyPress += new EventHandler(IsDirty);
or to any other event of your control that will mean changes are made
I recommend you bind an eventhandler to all your form's controls (by using a recursive foreach), that resets a timer each time the user performs some action. If you use some boolean value and then have a timer check that every 30 minutes, there's no way to guarantee that the user was really inactive for 30 minutes (maybe he was inactive for 59 minutes but only the second time the timer fired in that time the program would save).
Anyway, try this:
private static System.Timers.Timer inactiveTimer = new System.Timers.Timer(30*60*
public InitInactiveSave() {
// Make it threadsafe by keeping the timer callback in the same thread
// a System.Windows.Forms timer could do the same, but
// a System.Threading.Timer cannot!
inactiveTimer.Synchronizin
inactiveTimer. Elapsed += new System.Timers.ElapsedEvent
inactiveTimer.Start();
AddActivateEventHandlers(t
}
private void AddActivateEventHandlers(C
foreach(Control contained in control.Controls) {
// There are a lot of other events for control you could bind this to,
// but Click will do in this example
contained.Click += new EventHandler(OnControlActi
AddActivateEventHandlers(c
}
}
private void OnControlActivated(object sender, System.EventArgs e) {
// Reset the timer
inactiveTimer.Stop();
inactiveTimer.Start();
}
private void OnInactiveElapsed(object sender, System.Timers.ElapsedEvent
// TODO: Save stuff
}
Business Accounts
Answer for Membership
by: mandalorian4Posted on 2005-08-24 at 14:00:05ID: 14747104
chuang4630,
TimerCallback(AutoSave),nu ll,30000);
Try something like this.
System.Threading.Timer tm;
Private bool Activity;
//Somewhere in the form Load
tm = new System.Threading.Timer(new
Capture activity by subscribing to all the controls click/changed/etc event
Activity = true;
Private void AutoSave(Object state)
{
If(!Activity)
//Do your autosave here.
}
This timer will fire every 30 seconds if there has not been activity it will autosave