This article will explain how to monitor the time in which the user interacts with the application using the Windows API function.

Building the Sample

To run this example and need to have the framework 4.0 installed on your PC.


The example that we're going to implement it through the Visual Studio 2010, target framework 4.0.Tutto this is feasible as mentioned above using the library user32.dll present natively in the operating system Microsoft Windows.Il following code uses this library using the function GetLastInputInfo . GetLastInputInfo is a function only of the applicable software for Windows, (not on a website). GetLastInputInfo returns a Boolean value, if the function fails it returns zero or any value other than zero. This value is in milliseconds in the end the user's idle time with the application. The application on which you will perform the example of how technology like WPF, Microsoft VisualBasic language development Net.Diamo only a brief introduction to the technology we will use to create our example, WPF stands for Windows Presentation Foundation and was introduced in. NET Framework 3.0 was released in January 2007 with Windows Vista. WPF you can create desktop applications with attractive graphics using XAML, which is nothing but an XML (the base is) with the addition of many new objects including tags, styles, templates, and triggers a very altro.Per further study of this language online documentation on MSDN Library.Tornando to our example, once we open Microsoft Visual Studio 2010 and created the type of WPF application, will be created automatically by VisualStudio two rows, and MainWindow MainWindow.xaml . xaml.vb.Il first is the file that contains the markup, ie the graphics created using XAML, XAML, and said to remember that even declarative code, while the second is called code-behind file, with which we manage events objects created using XAML and other functions as needed. 

C#Visual Basic
Modifica script|Remove
// Call the framework dll 
using System; 
using System.Windows// On the last file that we import the following namespaces MainWindow.xaml.cs 
using System.Runtime.InteropServicesusing System.Windows.Threading; 
namespace TempoInattivitàUtenteApplicazione.Cs 
  /// <summary> 
  /// Logic of interaction for MainWindow.xaml 
  /// </ Summary> 
    public partial class MainWindow : Window 
        public MainWindow() 
// A module-level declare a variable of type integer, we call totalTime with initialized to zero. 
         // TotalTime we will indicate how many seconds we do not interact with keyboard and mouse. 
        int totaltime = 0; 
// Declare a new instance of the structure LASTINPUTINFO contained in GetLastInputInfo 
        LASTINPUTINFO lastInputInf = new LASTINPUTINFO(); 
// The following code calls the library user32.dll GetLastInputInfo and function. 
        public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); 
// The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. 
         // How can we find out within the term structure LASTINPUTINFO MarshalAs. 
         // Marshal is responsible to marshal data between managed and unmanaged code. 
        public struct LASTINPUTINFO 
            public int cbSize; 
            public int dwTime; 
// Loaded event of the MainWindow class. 
         // Inside the Loaded event is declared a new object DispatcherTimer 
         // combined with a Tick event, this event attracts every intervention method DisplayTime which updates an object label thus displaying the user's idle time with the application. 
         // Note that in WPF does not exist the Timer control, such control is available in the type applications WindowsForm, but as explained above being an application of type WPF, manage the whole with the control DispatcherTimer integrated in the namespace System.Windows . Threading. 
        private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) 
            DispatcherTimer dt = new DispatcherTimer(); 
            dt.Tick += dispatcherTimer_Tick; 
            dt.Interval = new TimeSpan(001); 
// Event object DispatcherTimer Tick 
         // Method is invoked DisplayTime () every second, it depends on the value range set in the Interval property of DispatcherTimer. 
        public void dispatcherTimer_Tick(object sender, EventArgs e) 
// Function GetLastInputTime. 
         // This function returns the variable idletime and assigns the value of idle time spent, 
         // All converted in seconds for the initial value retrieved from this intruzione 
         // code = idletime Environment.TickCount - lastInputInf.dwTime and expressed in milliseconds. 
         // Inside the class MainWindows, also declare a variable of type integer and call idletime which will be enhanced by Function GetLastInputTime. 
        public int GetLastInputTime() 
            int idletime = 0; 
            idletime = 0; 
            lastInputInf.cbSize = Marshal.SizeOf(lastInputInf); 
            lastInputInf.dwTime = 0; 
            if (GetLastInputInfo(ref lastInputInf)) 
                idletime = Environment.TickCount - lastInputInf.dwTime; 
            if (idletime != 0) 
                return idletime / 1000; 
                return 0; 
/ / Sub DisplayTime () 
         / / Sub This does nothing but display inside the value in seconds 
         / / the time in which you interact with your application. 
         // All () 'inside of this sub can handle it according to your needs and how to perform certain situations in the application, for example if one has previously performed a login by the user after a certain time of inactivity by the developer, you log back in again and resume the application which had been left by the user display of last time. 
        private void DisplayTime() 
            totaltime = GetLastInputTime(); 
            if (GetLastInputTime().Equals(1)) 
                Label1.Content = "Tempo di inattività pari a" + " " + GetLastInputTime().ToString() + " " + "secondo"; 
                Label1.Content = "Tempo di inattività pari a" + " " + GetLastInputTime().ToString() + " " + "secondi"; 
// Label1 visualizes the user's idle time. 
         // In a real application, the label will never be used as the count should never be shown to the user, this has been implemented only to show the actual operation of the application. After a period of time the application is disconnected and then the user must re login again to resume intragire with the application. 
         // For this purpose, with the Mouse Move event on Form MainWindow running inside the control variable totalTime, if more than five seconds is called LoginForm1 form and displays it as Modal Forms user, otherwise everything goes normanlemte without the application requires the user to login. 
        private void Window_MouseMove(System.Object sender, System.Windows.Input.MouseEventArgs e) 
            if (totaltime > 5) 
                FrmLogin log = new FrmLogin(); 

More Information

This is the first product born from the collaboration of Piero Sbressa and Carmelo La Monica.Potete contact the following references.
Piero Sbressa

Carmelo La Monica