Form close due to inactivity?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Form close due to inactivity?

Post by dan.potter »

Need to brainstorm. I have a form that I publish for end users to a terminal server..when I need to update it I have to go find who's left it open and close it via task manager.

I'm looking for ideas to track and possibly reset a timer based on any control interaction on the form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form close due to inactivity?

Post by jvierra »

In Windows we would hook the messageloop. PowerShell uses dialogs which do not own a message loop. There might be a generic process hook you could install like a keep-alive.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Form close due to inactivity?

Post by dan.potter »

This is what I came up with after a redbull. It works but I'm not sure if adding the mousehover event is proper, my idea was that I don't want the mousehover running all the time. Should I remove it after the timer is reset?
  1. $formmousehover = {
  2.    
  3.     $formO365UserProvision.Text = ''
  4.     $script:StartTime = (Get-Date).AddMinutes(1)
  5.     [TimeSpan]$span = $script:StartTime - (Get-Date)
  6.     $statusbar1.Text = "Form timeout: " + "{0:N0}" -f $span.TotalMinutes + " Minute(s)"
  7. }
  8.  
  9. $timer1_Tick = {
  10.    
  11.     [TimeSpan]$span = $script:StartTime - (Get-Date)
  12.    
  13.     if ($span.TotalSeconds -gt 30) { $statusbar1.Text = "Form timeout: " + "{0:mm}:{0:ss}" -f $span } else {
  14.                
  15.         $statusbar1.Text = "hover mouse over form to add time"
  16.         $formO365UserProvision.Text = "Are you still using this form? " + "{0:N0}" -f $span.TotalSeconds + " Seconds"
  17.        
  18.         $formO365UserProvision.add_MouseHover($formmousehover)
  19.        
  20.         if ($span.TotalSeconds -le 0) {
  21.            
  22.             $formO365UserProvision.Close()
  23.            
  24.         }
  25.        
  26.        
  27.     }
  28.    
  29.    
  30. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form close due to inactivity?

Post by jvierra »

That look like it might work. What if a process is running that takes a long time and the user is away from the system?
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Form close due to inactivity?

Post by dan.potter »

I wired up their chairs to give them 120v when they leave their rdp sessions disconnected :)

This one is just for adding/updating rows in a small sql db so the operations take seconds. I gave it a 10 minute timeout.
This topic is 7 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked