Lock program to taskbar after specified inactivity?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Lock program to taskbar after specified inactivity?

Post by Fangbeast »

Seems like I am asking more questions lately than doing anything but I am getting there. The constant heat and nearby fires sort of kill the enthusiasm a bit.

I want to start a thread in the background to monitor activity and when there is none for a user specified period, lock the program to the taskbar.

The thread and the lock I already do but how would I go about monitoring 'inactivity'?

1 Run a thread to monitor activity.
2 When there is any activity, the thread would restart again
3 If timeout is reached, lock to tray and kill thread.
4 When program is unlocked, start thread again.
Amateur Radio, D-STAR/VK3HAF
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Lock program to taskbar after specified inactivity?

Post by TI-994A »

Fangbeast wrote:...start a thread in the background to monitor activity and when there is none for a user specified period, lock the program to the taskbar...
Minimises the window after the specified period of inactivity (set to five seconds here):

Code: Select all

Global inactivityTimer, monitoringThread, appQuit

Procedure activityMonitor(*dummy)
  Repeat
    If ElapsedMilliseconds() - inactivityTimer > 5000
      SetWindowState(0, #PB_Window_Minimize)
      PauseThread(monitoringThread)      
    EndIf 
    Delay(250)
  Until appQuit
EndProcedure

OpenWindow(0, 0, 0, 400, 70, "Monitoring Inactivity",
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
monitoringThread = CreateThread(@activityMonitor(), 0)
inactivityTimer = ElapsedMilliseconds()

Repeat   
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow      
      appQuit = 1
    Case #PB_Event_RestoreWindow
      SetWindowState(0, #PB_Window_Normal)
      SetActiveWindow(0)      
      ResumeThread(monitoringThread)
  EndSelect
  inactivityTimer = ElapsedMilliseconds()  
Until appQuit
Same thing, but this one minimises the window to the system tray instead:

Code: Select all

Global inactivityTimer, monitoringThread, appQuit

Procedure activityMonitor(*dummy)
  Repeat
    If ElapsedMilliseconds() - inactivityTimer > 5000
      HideWindow(0, 1)
      PauseThread(monitoringThread)      
    EndIf 
    Delay(250)    
  Until appQuit
EndProcedure

sysTrayIcon$ = #PB_Compiler_Home + "examples\sources\Data\CdPlayer.ico"
sysTrayIcon = LoadImage(0, sysTrayIcon$)
OpenWindow(0, 0, 0, 400, 70, "Monitoring Inactivity",
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddSysTrayIcon(0, WindowID(0), sysTrayIcon) 
SysTrayIconToolTip(0, "My Application") 
monitoringThread = CreateThread(@activityMonitor(), 0)
inactivityTimer = ElapsedMilliseconds()

Repeat   
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow      
      appQuit = 1
    Case #PB_Event_SysTray
      HideWindow(0, 0)      
      SetActiveWindow(0)      
      ResumeThread(monitoringThread)
  EndSelect
  inactivityTimer = ElapsedMilliseconds()  
Until appQuit
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Lock program to taskbar after specified inactivity?

Post by Fangbeast »

TI-994A wrote:
Fangbeast wrote:...start a thread in the background to monitor activity and when there is none for a user specified period, lock the program to the taskbar...
Minimises the window after the specified period of inactivity (set to five seconds here):

Code: Select all

Global inactivityTimer, monitoringThread, appQuit

Procedure activityMonitor(*dummy)
  Repeat
    If ElapsedMilliseconds() - inactivityTimer > 5000
      SetWindowState(0, #PB_Window_Minimize)
      PauseThread(monitoringThread)      
    EndIf 
    Delay(250)
  Until appQuit
EndProcedure

OpenWindow(0, 0, 0, 400, 70, "Monitoring Inactivity",
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
monitoringThread = CreateThread(@activityMonitor(), 0)
inactivityTimer = ElapsedMilliseconds()

Repeat   
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow      
      appQuit = 1
    Case #PB_Event_RestoreWindow
      SetWindowState(0, #PB_Window_Normal)
      SetActiveWindow(0)      
      ResumeThread(monitoringThread)
  EndSelect
  inactivityTimer = ElapsedMilliseconds()  
Until appQuit
Same thing, but this one minimises the window to the system tray instead:

Code: Select all

Global inactivityTimer, monitoringThread, appQuit

Procedure activityMonitor(*dummy)
  Repeat
    If ElapsedMilliseconds() - inactivityTimer > 5000
      HideWindow(0, 1)
      PauseThread(monitoringThread)      
    EndIf 
    Delay(250)    
  Until appQuit
EndProcedure

sysTrayIcon$ = #PB_Compiler_Home + "examples\sources\Data\CdPlayer.ico"
sysTrayIcon = LoadImage(0, sysTrayIcon$)
OpenWindow(0, 0, 0, 400, 70, "Monitoring Inactivity",
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
AddSysTrayIcon(0, WindowID(0), sysTrayIcon) 
SysTrayIconToolTip(0, "My Application") 
monitoringThread = CreateThread(@activityMonitor(), 0)
inactivityTimer = ElapsedMilliseconds()

Repeat   
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow      
      appQuit = 1
    Case #PB_Event_SysTray
      HideWindow(0, 0)      
      SetActiveWindow(0)      
      ResumeThread(monitoringThread)
  EndSelect
  inactivityTimer = ElapsedMilliseconds()  
Until appQuit
Woohoo!! Thank you! That's great. I'm still adding the font substitutes routine to all my other windows and than I can do this. Maybe even release an updated MyInfo this month! (I hope)
Amateur Radio, D-STAR/VK3HAF
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Lock program to taskbar after specified inactivity?

Post by Mijikai »

Heres some code that uses the WinApi to se if there is some inactivity.
Its global and not tied to a window.

Code:

Code: Select all

Procedure.i IsInactive(TimeOut.i)
  Protected lii.LASTINPUTINFO
  If GetLastInputInfo_(@lii)
    ProcedureReturn Bool(GetTickCount_() - lii\dwTime) > TimeOut)
  EndIf
EndProcedure
Post Reply