2

Windows Form Application:

Is there a way to check the time for which the device (Microsoft Surface 55 inch touchscreen) hasn't received any interaction (touch) from the user and if it is more than 5 mins than a confirmation box should be displayed asking whether you want to continue or exit. For this 5 mins either a flash file or a video will be running. If the video is running (I have embedded AxWindowsMediaPlayer in my Win Form) then it should be paused and then the confirmation box should be displayed.

I want to achieve this using C# code on Visual Studio 2013 Professional.

I came across a few functions and threads:

One of the option i found is to use GetLastInputInfo() function. However, I am not sure whether it will work for touch interactivity. I don't have a Microsoft Surface device to test. A quote from MSDN:

This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function. The tick count when the last input event was received (see LASTINPUTINFO) is not guaranteed to be incremental. In some cases, the value might be less than the tick count of a prior event. For example, this can be caused by a timing gap between the raw input thread and the desktop thread or an event raised by SendInput, which supplies its own tick count.

I also came across Application.idle event

Anyone has a workaround or useful links for this will be highly appreciated.

Yash Saraiya
  • 1,035
  • 1
  • 20
  • 40
  • You could use a timer to run in the background, have a tick every minute and on minute >= 5 launch your confirmation box etc. Then use the Form.Click() event and reset the timer to zero on every click. This is just a thought. Not sure if it's feasible for your app. The touchscreen shouldn't make any difference since the touch of the finger just simulates a mouse click. – Timmy Nov 04 '15 at 16:49
  • So anything I code for a click will work for a touch as well? – Yash Saraiya Nov 04 '15 at 17:36
  • Yes, at least in my experience that's how it has always worked. We use a lot of touchscreens in the place I work. Using a normal click event on a button for example works all the same if I click the button with my mouse or "touch" the button with my finger on the screen. – Timmy Nov 04 '15 at 19:23
  • What does this quote for Application.idle event on MSDN mean: "Because this is a static event, you must detach your event handlers when your application is disposed, or memory leaks will result". – Yash Saraiya Nov 05 '15 at 06:00
  • Check out this thread http://stackoverflow.com/questions/7812627/application-threadexception-memory-leak-if-not-detached – Timmy Nov 05 '15 at 12:57

1 Answers1

2

This piece of code did the trick for me:

private void timer1_Tick(object sender, EventArgs e)
    {           
        tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
        Int32 IdleTime;
        LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
        LastInput.dwTime = 0;

        if (GetLastInputInfo(ref LastInput))
        {
            IdleTime = System.Environment.TickCount - LastInput.dwTime;
            if (IdleTime > 10000)
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                timer1.Stop();
                string timeRemaining = IdleTime.ToString();
                lbl_TimeRemaining.Text = IdleTime.ToString();
                lbl_TimeRemaining.Show();
                MessageBox.Show("Do you wish to continue?");
                lbl_TimeRemaining.Hide();
            }
            else
            {

            }
            timer1.Start();
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }            
        string title = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
    }

For the first time you will have to start the timer by calling timer1.start(); from inside the FormLoad() method

Yash Saraiya
  • 1,035
  • 1
  • 20
  • 40