Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3951666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:45:28+00:00 2026-05-20T01:45:28+00:00

I have an MVVM kiosk application that I need to restart when it has

  • 0

I have an MVVM kiosk application that I need to restart when it has been inactive for a set amount of time. I’m using Prism and Unity to facilitate the MVVM pattern. I’ve got the restarting down and I even know how to handle the timer. What I want to know is how to know when activity, that is any mouse event, has taken occurred. The only way I know how to do that is by subscribing to the preview mouse events of the main window. That breaks MVVM thought, doesn’t it?

I’ve thought about exposing my window as an interface that exposes those events to my application, but that would require that the window implement that interface which also seems to break MVVM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:45:29+00:00Added an answer on May 20, 2026 at 1:45 am

    Another option is to use the Windows API method GetLastInputInfo.

    Some cavets

    • I’m assuming Windows because it’s WPF
    • Check if your kiosk supports GetLastInputInfo
    • I don’t know anything about MVVM. This method uses a technique that is UI agnostic, so I would think it would work for you.

    Usage is simple. Call UserIdleMonitor.RegisterForNotification. You pass in a notification method and a TimeSpan. If user activity occurs and then ceases for the period specified, the notification method is called. You must re-register to get another notification, and can Unregister at any time. If there is no activity for 49.7 days (plus the idlePeriod), the notification method will be called.

    public static class UserIdleMonitor
    {
        static UserIdleMonitor()
        {
            registrations = new List<Registration>();
            timer = new DispatcherTimer(TimeSpan.FromSeconds(1.0), DispatcherPriority.Normal, TimerCallback, Dispatcher.CurrentDispatcher);
        }
    
        public static TimeSpan IdleCheckInterval
        {
            get { return timer.Interval; }
            set
            {
                if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
                    throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
                timer.Interval = value;
            }
        }
    
        public sealed class Registration
        {
            public Action NotifyMethod { get; private set; }
            public TimeSpan IdlePeriod { get; private set; }
            internal uint RegisteredTime { get; private set; }
    
            internal Registration(Action notifyMethod, TimeSpan idlePeriod)
            {
                NotifyMethod = notifyMethod;
                IdlePeriod = idlePeriod;
                RegisteredTime = (uint)Environment.TickCount;
            }
        }
    
        public static Registration RegisterForNotification(Action notifyMethod, TimeSpan idlePeriod)
        {
            if (notifyMethod == null)
                throw new ArgumentNullException("notifyMethod");
            if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
                throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
    
            Registration registration = new Registration(notifyMethod, idlePeriod);
    
            registrations.Add(registration);
            if (registrations.Count == 1)
                timer.Start();
    
            return registration;
        }
    
        public static void Unregister(Registration registration)
        {
            if (registration == null)
                throw new ArgumentNullException("registration");
            if (Dispatcher.CurrentDispatcher != timer.Dispatcher)
                throw new InvalidOperationException("UserIdleMonitor can only be used from one thread.");
    
            int index = registrations.IndexOf(registration);
            if (index >= 0)
            {
                registrations.RemoveAt(index);
                if (registrations.Count == 0)
                    timer.Stop();
            }
        }
    
        private static void TimerCallback(object sender, EventArgs e)
        {
            LASTINPUTINFO lii = new LASTINPUTINFO();
            lii.cbSize = Marshal.SizeOf(typeof(LASTINPUTINFO));
            if (GetLastInputInfo(out lii))
            {
                TimeSpan idleFor = TimeSpan.FromMilliseconds((long)unchecked((uint)Environment.TickCount - lii.dwTime));
                //Trace.WriteLine(String.Format("Idle for {0}", idleFor));
    
                for (int n = 0; n < registrations.Count; )
                {
                    Registration registration = registrations[n];
    
                    TimeSpan registeredFor = TimeSpan.FromMilliseconds((long)unchecked((uint)Environment.TickCount - registration.RegisteredTime));
                    if (registeredFor >= idleFor && idleFor >= registration.IdlePeriod)
                    {
                        registrations.RemoveAt(n);
                        registration.NotifyMethod();
                    }
                    else n++;
                }
    
                if (registrations.Count == 0)
                    timer.Stop();
            }
        }
    
        private static List<Registration> registrations;
        private static DispatcherTimer timer;
    
        private struct LASTINPUTINFO
        {
            public int cbSize;
            public uint dwTime;
        }
    
        [DllImport("User32.dll")]
        private extern static bool GetLastInputInfo(out LASTINPUTINFO plii);
    }
    

    Updated

    Fixed issue where if you tried to re-register from the notification method you could deadlock.

    Fixed unsigned math and added unchecked.

    Slight optimization in timer handler to allocate notifications only as needed.

    Commented out the debugging output.

    Altered to use DispatchTimer.

    Added ability to Unregister.

    Added thread checks in public methods as this is no longer thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Silverlight app using the MVVM pattern. I have a main view
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a n-tire web application and search often times out after 30 secs. How
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
Have you managed to get Aptana Studio debugging to work? I tried following this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.