0

I found this post and created a class that used it to detect inactive time and it works great. I set it for one minute and after one minute I can get it to "do stuff". What I am trying to do is only do something every "x" minutes of inactive time; i.e. every 5 minutes do this if things have been inactive and do not repeat again 'til X time has elapsed.

Now, I could set my timer to fire every 5 minutes instead of every second, but I would like to be able to "reset" the count of inactive time instead. Any suggestions?

This is for using the DispatchTimer in C# and WPF.

Community
  • 1
  • 1
Wayne In Yak
  • 546
  • 1
  • 4
  • 21
  • 1
    So having C# ad WPF in the title is a no-no? How come there are so many other posts on this very board with C# and WPF in their titles? – Wayne In Yak Sep 24 '13 at 22:47
  • Because they're naughty. Try to avoid tags in titles unless it would make the title not flow well without. – dav_i Sep 26 '13 at 13:33
  • If you use GetLastInputInfo (user32.dll) to calc the inactive time, then you may use SendInput (user32.dll) as well. Check the accepted answer at http://stackoverflow.com/questions/4963135/wpf-inactivity-and-activity/4965166#4965166 (VB.NET) or http://homeofcox-cs.blogspot.gr/2008/07/c-simulate-mouse-and-keyboard-events.html for C# code –  Sep 28 '13 at 00:08
  • I'm having difficulty parsing your question. Are you asking for: `if (inactive for x minutes) { every 5 minutes do this } else { do nothing }` ? – unsigned long double Oct 01 '13 at 16:21
  • I'm basically asking how to reset the inactivity count after a desired period. Say I'm checking every 10 seconds for inactivity. After 5 minutes I want to "do something". Now I will keep checking every 10 seconds for inactivity but don't want to do anything until another 5 minutes has passed. The way it is currently working after 5 minutes I "do something" but then every 10 seconds later I keep "doing something". – Wayne In Yak Oct 01 '13 at 16:36
  • Without redesigning how you have things too much, why not a secondary timer with an interval of five minutes that is started or stopped upon the shorter timer's idle state detection? Edit: The method would of course have to be called once: short timer detects idle, calls method, starts 5 minute timer. – unsigned long double Oct 01 '13 at 16:43

2 Answers2

1

Just create a class level variable, increment it on your timer, and reset it when you get activity. Create a timer, say tmrDelay with an increment of 10000 milliseconds, and a button, btnActivity to reset the count, and do this:

private int tickCount = 0;
private const int tick_wait = 30; 

private void tmrDelay_Tick(object sender, EventArgs e)
{ 
    tickCount++; 

    if (tickCount > tick_wait)
    {
        DoSomething();

        tickCount = 0;   
    }
}
private void btnActivity_Click(object sender, EventArgs e)
{
    tickCount = 0;
}
  • Basically what I was going to say. Wayne, everytime your wpf form is able to detect user input (mouse movement, key press), call a method that sets `tickCount = 0;` again. –  Oct 01 '13 at 18:20
  • Thanks, was a duh moment for me, simple solution and it worked – Wayne In Yak Oct 01 '13 at 22:17
0

It sounds like you want something like the following:

static DispatcherTimer dt = new DispatcherTimer();

static LastInput()
{
    dt.Tick += dt_Tick;
}

static void dt_Tick(object sender, EventArgs e)
{
    var timer = (DispatcherTimer)sender;
    var timeSinceInput = TimeSpan.FromTicks(GetLastInputTime());

    if (timeSinceInput < TimeSpan.FromMinutes(5))
    {
        timer.Interval = TimeSpan.FromMinutes(5) - timeSinceInput;
    }
    else
    {
        timer.Interval = TimeSpan.FromMinutes(5);
        //Do stuff here
    }
}

This will poll every 5 minutes to see if the system has been idle for 5 minutes or more. If it's been idle for less than 5 minutes it will adjust the time so that it will go off again at exactly the 5 minute mark. Obviously then if there has been activity since the timer was set it will be adjusted again so it will always aim for 5 minutes of idleness.

If you really want to reset the active time then you will actually need to trigger some activity either by moving the mouse or sending a keypress

Community
  • 1
  • 1
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49