0

I have a vb.net Application and I need to make my Application to go sleep if not used 10 minutes or 15 minutes? I am trying to use Stopwatch and Timer

//Here is the Main form load

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    StopWatch.Start()
    Tmr.Start()
End Sub

When the Main Form loads Stopwatch was Starting

//here is the Timer Tick

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    Try
        Label2.Text = StopWatch.Elapsed.ToString
        If StopWatch.Elapsed.Minutes = 4 Then
            Sleep.Show()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    End Try
End Sub

I need when the Time reaches 10 Min to show the user another Form called Sleep and enter his username and password after that come back to his work and continue

Pop up of the Sleep Form

can any body help me?

Sorry for my bad English.

Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
  • 1
    What have _you_ tried? And where did you fail, with what kind of message? Please have a look at [mcve]. – rickvdbosch Apr 30 '18 at 12:43
  • 1
    Seems like you already got your popup. Maybe you are instead looking for a way to detect any activity in your application and then restart the timer? – the_lotus Apr 30 '18 at 12:44
  • The problem is how i can count the time that user does not uses the application – Jimale Abdi Apr 30 '18 at 12:45
  • Are you sure implementing this in the application is the best approach? There are already mechanisms in windows to lock the *entire workstation* due to inactivity and to require password to unlock. Why does your application need to duplicate this/more tightly constrain usage? – Damien_The_Unbeliever Apr 30 '18 at 13:11
  • Damien_The_Unbeliever This Application is School Management System so when we look our Country it needs to be more Secure because of some times the Administrator leaves the office if some thing happen such as if two students are fighting so i need to secure it – Jimale Abdi Apr 30 '18 at 13:25
  • @ZADICKEDEÑ - but again, why not let *windows* lock down the entire workstation, rather than just trying to lock *your application*? – Damien_The_Unbeliever Apr 30 '18 at 13:57
  • Some of the Computer are not locked down Automatically or disabled so i need my own. – Jimale Abdi Apr 30 '18 at 14:19

1 Answers1

1

I'm assuming your issue is that you want the timer to start when the user is actually AFK.

I implemented something similar a few years ago using the IMessageFilter interface.

There's a good example in this post.

I'd say you can just replace the

MessageBox.Show("Time is up!")

with your

Sleep.Show()

You may want to get rid of the ControlBox in your Sleep form, and maybe change .Show with .ShowDialog()

Hope it helps

trick
  • 78
  • 1
  • 9
  • i am trying to .ShowDialog() and that is the best way because of user can't open other forms until he enters his Username and Password but there is a problem when i change .Show to .ShowDialog() – Jimale Abdi Apr 30 '18 at 12:56
  • 1
    @ZADICKEDEÑ : _"but there is a problem"_ doesn't describe anything useful. _**What**_ is the problem? – Visual Vincent Apr 30 '18 at 13:05
  • The problem is Form that is already visible cannot be displayed as a modal dialog box, set the form's visible property to false before calling showDialog. – Jimale Abdi Apr 30 '18 at 13:15
  • 1
    I'm guessing the Tick event is fired more than once because you're missing the Timer2.Stop and Timer2.Enable = false in the Tick event. Just stop it when the Tick event is fired and start it again after the .ShowDialog() returns. – trick Apr 30 '18 at 13:18
  • 1
    @wannabe thank you so much it works me – Jimale Abdi Apr 30 '18 at 13:34
  • 1
    @wannabe : You only need to call `Timer2.Stop()` as it sets `Enabled = False` internally. – Visual Vincent Apr 30 '18 at 13:41
  • @wannabe suppose the user leaves the application and come after 9 minutes and used the Application, i need to restart the stopwatch if i use keypress , MouseClick etc its hard because i have more Forms so what you advice to me ? – Jimale Abdi Apr 30 '18 at 13:43
  • 1
    @ZADICKEDEÑ : If you use and examine the answer that wannabe linked to in his post (the second link) you'll see how it's done. – Visual Vincent Apr 30 '18 at 13:44
  • @ZADICKEDEÑ the IMessageFilter interface does that for you. Check the PreFilterMessage function content in the post I've linked before [VB Detect Idle time](https://stackoverflow.com/questions/12636850/vb-detect-idle-time). – trick Apr 30 '18 at 13:46
  • @ Visual Vincent thank you more and more – Jimale Abdi Apr 30 '18 at 13:52