WPF long time unattended operation

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In the process of software development in order to safety, especially those procedures that need to use a user name and password login server, often consider long-term unattended operation, the program automatically jump to the user login interface.

To determine if a program long time unattended operation, there are two bases, the first one is the long time does not move, the second is the mouse focus long time not in this program (which later time in operating other programs).

A long time, the mouse did not move

In the other blog seen in solution [mouse long time does not move it 1], refer to this blog, add the appropriate code to the App.xaml.cs (code), this paper is the realization of the mouse for a long time does not move (set 10s in this example does not move) then restart this program (because of the need to login the account password procedures, the mouse for a long time does not move to the exit sign, and restart to the login interface, so as to ensure the safety).

/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private DispatcherTimer mousePositionTimer;    //Long time not to operate the program back to the login screen.
        private Point mousePosition;    //The location of the mouse

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);  //Start the program
            mousePosition = GetMousePoint();  //To obtain the mouse coordinates

            mousePositionTimer = new DispatcherTimer();
            mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
            mousePositionTimer.Interval = new TimeSpan(0, 0, 10);     //Every 10 seconds a mouse position change detection
            mousePositionTimer.Start();
        }

        private void MousePositionTimedEvent(object sender, EventArgs e)
        {
            if (!HaveUsedTo())
            {                                    
                mousePositionTimer.Stop();
                //Restart the program
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
            }
        }

        //Determine whether the mouse movement
        private bool HaveUsedTo()
        {
            Point point = GetMousePoint();
            if (point == mousePosition)
            {
                return false;
            }
            mousePosition = point;
            return true;
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct MPoint
        {
            public int X;
            public int Y;

            public MPoint(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool GetCursorPos(out MPoint mpt);
 
        /// Gets the current mouse position   
        public Point GetMousePoint()
        {
            MPoint mpt = new MPoint();
            GetCursorPos(out mpt);
            Point p = new Point(mpt.X, mpt.Y);
            return p;
        } 
    }
But this solution has a problem, as shown in Figure 1, the 10s timer interval hypothesis, the starting point for the mouse coordinates (0,0), if not the mouse 10s, then to 10s timer will restart procedure. However, when time is at 1s, we move the mouse (assuming the move to (0,1)), then again the mouse for a long time does not move, when the time to 10s mouse compared with 0s moments, the coordinates of the mouse is changed, so as not to execute the timer event, the program continues the 10s timing, if the next 10s mouse don't move, is reaching the 20s timer events will respond to restart the program operation. This program is actually experienced 19S to restart, did not achieve the 10s mouse is not moving the program restart request.


Figure 1


The improved solution:

After improvement, to achieve the same 10s mouse is not moving the program restart requirements, we design the interval timer 1s, and add the counter the mouse didn't move, the counter reaches the 10 executive program restart. Realization is this: every 1s detection of the mouse is mobile, without moving the counter plus 1, if midway mouse movement, the counter reset, to counter the count to 10, then to the mouse 10 mouse detection are not moving, so from the mouse stops moving, to counter reached 10, just 10s the mouse is not moving, can reach 10s program restart request. The concrete realization of the code are as follows (note that this code is added in App.xaml.cs):


/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private DispatcherTimer mousePositionTimer;    //Long time not to operate the program back to the login screen.
        private Point mousePosition;    //The location of the mouse
        private int checkCount = 0;   //The number of the mouse position detection

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);  //Start the program
            mousePosition = GetMousePoint();  //To obtain the mouse coordinates

            mousePositionTimer = new DispatcherTimer();
            mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
            mousePositionTimer.Interval = new TimeSpan(0, 0, 1);     //Every 10 seconds a mouse position change detection
            mousePositionTimer.Start();
        }

        private void MousePositionTimedEvent(object sender, EventArgs e)
        {
            if (!HaveUsedTo())
            {
                checkCount++;    //Mouse detected no mobile, checkCount + 1  
                if (checkCount == 10)
                {
                    checkCount = 0;
                    mousePositionTimer.Stop();
                    //Restart the program
                    System.Windows.Forms.Application.Restart();
                    Application.Current.Shutdown();
                }

            }
            else
            {
                checkCount = 0;     //Detection of mouse movement, re count
            }
        }

        //Determine whether the mouse movement
        private bool HaveUsedTo()
        {
            Point point = GetMousePoint();
            if (point == mousePosition)
            {
                return false;
            }
            mousePosition = point;
            return true;
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct MPoint
        {
            public int X;
            public int Y;

            public MPoint(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool GetCursorPos(out MPoint mpt);
 
        /// Gets the current mouse position   
        public Point GetMousePoint()
        {
            MPoint mpt = new MPoint();
            GetCursorPos(out mpt);
            Point p = new Point(mpt.X, mpt.Y);
            return p;
        } 
    }


Two, the mouse focus long time not in this program

To use the Activated and Deactivated events [2]: Activated event is triggered when a top level form in Application receives the focus, is also the application is activated. The Deactivated event is triggered when the user to other application from the application program switching. The following code:

Accession to the Activated and Deactivated events in App.xaml,

App.xaml.cs:


/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private DispatcherTimer deactivatedTimer;   //When the focus is not on this program timer

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);  //Start the program
            deactivatedTimer = new DispatcherTimer();
            deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick);
            deactivatedTimer.Interval = new TimeSpan(0, 0, 10);   //If the focus is not on this program, 10s program, automatic restart
        }

        private void deactivatedTimer_Tick(object sender, EventArgs e)
        {
            deactivatedTimer.Stop();
            //Restart the program
            System.Windows.Forms.Application.Restart();
            Application.Current.Shutdown();
        }

        private void Application_Activated(object sender, EventArgs e)
        {
            deactivatedTimer.Stop();
        }

        private void Application_Deactivated(object sender, EventArgs e)
        {
            deactivatedTimer.Start();
        }
        
    }


Three, two kinds of situation

This paper initially have been proposed, to determine if a program there are two bases long time unattended operation, namely the mouse for a long time without moving the mouse focus and long time not in this program, so this paper two kinds of cases, do the real long time response program to realize unmanned operation. The following code:

Accession to the Activated and Deactivated events in App.xaml,

App.xaml.cs:


/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private DispatcherTimer mousePositionTimer;    //Long time not to operate the program back to the login screen.
        private Point mousePosition;    //The location of the mouse
        private int checkCount = 0;   //The number of the mouse position detection

        private DispatcherTimer deactivatedTimer;   //When the focus is not on this program timer

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);  //Start the program
            mousePosition = GetMousePoint();  //To obtain the mouse coordinates

            mousePositionTimer = new DispatcherTimer();
            mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
            mousePositionTimer.Interval = new TimeSpan(0, 0, 1);     //Every 10 seconds a mouse position change detection
            mousePositionTimer.Start();

            deactivatedTimer = new DispatcherTimer();
            deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick);
            deactivatedTimer.Interval = new TimeSpan(0, 0, 10);   //If the focus is not on this program, 10s program, automatic restart
        }

        private void MousePositionTimedEvent(object sender, EventArgs e)
        {
            if (!HaveUsedTo())
            {
                checkCount++;    //Mouse detected no mobile, checkCount + 1  
                if (checkCount == 10)
                {
                    checkCount = 0;
                    mousePositionTimer.Stop();
                    //Restart the program
                    System.Windows.Forms.Application.Restart();
                    Application.Current.Shutdown();
                }

            }
            else
            {
                checkCount = 0;     //Detection of mouse movement, re count
            }
        }

        private void deactivatedTimer_Tick(object sender, EventArgs e)
        {
            deactivatedTimer.Stop();
            //Restart the program
            System.Windows.Forms.Application.Restart();
            Application.Current.Shutdown();
        }

        //The mouse focus back to the program
        private void Application_Activated(object sender, EventArgs e)
        {
            mousePositionTimer.Start();
            deactivatedTimer.Stop();
        }

        //The mouse focus away from this program
        private void Application_Deactivated(object sender, EventArgs e)
        {
            mousePositionTimer.Stop();
            deactivatedTimer.Start();
        }

        //Determine whether the mouse movement
        private bool HaveUsedTo()
        {
            Point point = GetMousePoint();
            if (point == mousePosition)
            {
                return false;
            }
            mousePosition = point;
            return true;
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct MPoint
        {
            public int X;
            public int Y;

            public MPoint(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool GetCursorPos(out MPoint mpt);

        /// Gets the current mouse position   
        public Point GetMousePoint()
        {
            MPoint mpt = new MPoint();
            GetCursorPos(out mpt);
            Point p = new Point(mpt.X, mpt.Y);
            return p;
        } 

    }



Reference resources:

[1]

[2]


Code download





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Upton at December 09, 2013 - 1:14 PM