timeout for windows app

I have a C# Windows application that displays web pages in form windows.  Basically the first page that is displayed is used to lauch other pages which will also be displayed in windows.  How do I code a timeout?  
PeterTrefrenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
I believe you use winforms WebBrowser to view pages. What timeout you are talking about?
0
PeterTrefrenAuthor Commented:
I can display the page using the WebBrowser control however I want to have the application log the user out if they haven't used it after a period of time.  I'm trying to track how much time they are using the app.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you want the Windows App itself to close down after a certain time?

Add a standard Timer control from the ToolBox (the 'Components' section by default) to your form and then double click it to the get the handler:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = (int)TimeSpan.FromMinutes(30).TotalMilliseconds; // shut down after 30 minutes
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close(); // close down the main form (and consequently the app)
        }

    }

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

anarki_jimbelSenior DeveloperCommented:
No, I think PeterTrefren means probably more disconnecting from internet. Or logging off an application.

I.e., user logs in, uses Internet, if application is not used - logg user off and disconnect from internet.
Is that correct?.

0
PeterTrefrenAuthor Commented:
This application sits on the users desktop.  When the user runs it they enter a login and then are presented with a HTML page of training related material.  This page comes from a local IIS server within a company.  This page will offer links that when clicked on will open up other windows with HTML content.  Because I know all of the links the user can click on I can assess where they are spending their time.  However if they walk away from the machine I would like to know to shut the application down.  I suppose if I keep reseting the timer on the main page every time the user interacted with any of the forms in the app that might work.  I'll think about it and see how it works at the office tomorrow.

Peter
0
anarki_jimbelSenior DeveloperCommented:
Yes, it's easy, I think, to reset a timer when document is loaded. But it's tricky to check further activity: lot of events for Control class are not supported for a browser...
0
CodeCruiserCommented:
Try this for detecting inactivity and then use timer to close the app. Reset the timer everytime activity is detected

http://www.codeproject.com/KB/cs/uim.aspx

This one is for WPF but should clear some concepts

http://www.codeproject.com/KB/WPF/AutologoffWPF.aspx


Also
http://www.blackwasp.co.uk/InactivityDetection.aspx


Or best of all, use the class from this thread
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26768907.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterTrefrenAuthor Commented:
All,

I'm in the process of converting the UserIdle example over to C# suggested by CodeCrusier.  It will take a little bit of time.  Just to let you know that I'm still working on the solution.

Thanks,

Peter
0
PeterTrefrenAuthor Commented:
CodeCrusier,

The solution I'm going with is the one you recommend by eql1044 using the UserIdle class.  Unfortunately I've only been able to get it to work in a VB .NET project.  If anyone knows how to translate the code into C# that would be very helpful to me.

Thanks,

Peter
'// Usage

Public Class Form1
    Dim WithEvents idle As New UserIdle(1)
    Private Sub idle_OnIdle(ByVal sender As Object, ByVal e As IdleEventArgs) Handles idle.OnIdle
        Select Case e.State
            Case IdleEventArgs.UserIdleState.UserIdleBegin
                Debug.Print("User Idle - {0}", e.TimeStamp)
            Case IdleEventArgs.UserIdleState.UserIdleEnd
                Debug.Print("User UnIdle - {0}", e.TimeStamp)
        End Select
    End Sub
End Class

Open in new window

Imports System.Runtime.InteropServices

Public Class IdleEventArgs
    Inherits EventArgs
    Public Enum UserIdleState As Integer
        UserIdleBegin = 1
        UserIdleEnd = 2
    End Enum
    Private _idleState As Integer = 0
    Public Sub New(ByVal idleState As Integer)
        Me._idleState = idleState
    End Sub
    Public ReadOnly Property State As UserIdleState
        Get
            Return _idleState
        End Get
    End Property
    Public ReadOnly Property TimeStamp As String
        Get
            Return Date.Now.ToLocalTime.ToString
        End Get
    End Property
    'TODO: Add more stuff...
End Class 'IdleEventArgs

Public Class UserIdle
    Public Delegate Sub IdleEventHandler(ByVal sender As Object, ByVal e As IdleEventArgs)
    Public Shared Event OnIdle As IdleEventHandler
    Private bIsRunning As Boolean = False
    Public Sub New(ByVal idleMinutes As Integer)
        Me.BeginDetection(idleMinutes)
    End Sub
    Protected Overrides Sub Finalize()
        Me.EndDetection()
        MyBase.Finalize()
    End Sub
    Private Sub BeginDetection(ByVal idleMinutes As Integer)
        '   Begins monitoring inactivity.
        If Not bIsRunning Then
            If SafeNativeMethods.BeginIdleDetection(New IdleCallbackDelegate(AddressOf IdleCallback), idleMinutes, 0) = 0 Then
                bIsRunning = True
            End If
        End If
    End Sub
    Public Sub EndDetection()
        '   Ends monitoring inactivity.
        If bIsRunning Then
            If SafeNativeMethods.EndIdleDetection(0) Then
                bIsRunning = False
            End If
        End If
    End Sub
    Public Function GetIdleMinutes() As Integer
        '   Gets the length of time, in minutes, since the user's last activity.
        Return SafeNativeMethods.GetIdleMinutes(0)
    End Function
    Private Delegate Sub IdleCallbackDelegate(ByVal dwState As Integer)
    Private Sub IdleCallback(ByVal dwState As Integer)
        Dim idleArg As New IdleEventArgs(dwState)
        RaiseEvent OnIdle(Me, idleArg)
    End Sub
    Public ReadOnly Property IsRunning As Boolean
        Get
            Return bIsRunning
        End Get
    End Property
End Class 'UserIdle

Friend Class SafeNativeMethods
    Private Sub New()
    End Sub
    <DllImport("msidle.dll", SetLastError:=True, ExactSpelling:=False, EntryPoint:="#3")> _
    Friend Shared Function BeginIdleDetection(ByVal pfnCallback As [Delegate], ByVal dwIdleMin As Integer, ByVal dwReserved As Integer) As Integer
    End Function
    <DllImport("msidle.dll", SetLastError:=True, ExactSpelling:=False, EntryPoint:="#4")> _
    Friend Shared Function EndIdleDetection(ByVal dwReserved As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("msidle.dll", SetLastError:=True, ExactSpelling:=False, EntryPoint:="#8")> _
    Friend Shared Function GetIdleMinutes(ByVal dwReserved As Integer) As Integer
    End Function
End Class

Open in new window

0
CodeCruiserCommented:
Did you get it working?
0
PeterTrefrenAuthor Commented:
I'm still working on it.  This item is something that I need for a project that is due in about 3 weeks.  Right now I need to work on a different project.  I'll probably look at it this weekend or next week when I'm on the road.  When I figure it out I'll post the solution.  I think its important to have a version of this in C#.  Its a pretty useful piece of code.
0
CodeCruiserCommented:
Ok. Let us know if you need any more help.
0
PeterTrefrenAuthor Commented:
Does anyone how I would translate the following code to C# based on the solution I'm working on:

If SafeNativeMethods.BeginIdleDetection(New IdleCallbackDelegate(AddressOf IdleCallback), idleMinutes, 0) = 0 Then

I'm not sure how to handle the new IdleCallbackDelegate(AddressOf IdleCallback) portion.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Usually you just use the name of the method in C# (without any equivalent for "AddressOf"):

    new IdleCallbackDelegate(IdleCallback)
0
PeterTrefrenAuthor Commented:
When I do that I get an error of cannot convert IdleCallbackDelegate to System.Delegate[].
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.