Jump to content

Running a command once every X minutes but keeping the program active.


 Share

Recommended Posts

Hello,

I'm having trouble using a timer for my program. It should work as follows:

- a function is ran once every X minutes (user defined)

- the program should still be available for interacting - ONLY by System Tray, using TrayGetMsg.

I don't need to use TimerInit because the program runs at 100%.

Thanks,

mexykanu.

Link to comment
Share on other sites

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1); Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()


While 1
    $dif=TimerDiff($start)
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"About:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect



WEnd
Exit

Func _givemsg()
    MsgBox(0,"","msg")
EndFunc

Hi again. Here's a sample code. I want to run _givemsg() every 2 minutes.

Thanks,

mexykanu.

Link to comment
Share on other sites

  • Moderators

mexykanu,

You are not using TimerDiff correctly. This will do what you want:

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1); Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()

$start = TimerInit()

While 1
    If TimerDiff($start) > 2 * 60 * 1000 Then
        _givemsg()
        $start = TimerInit()
    EndIf
    
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"About:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect

WEnd
Exit

Func _givemsg()
    MsgBox(0,"","msg")
EndFunc

This will restart the 2 minute delay from the moment you close the message box. If you want the box to open exactly 2 minutes after the last time it opened, just place the new TimerInit line above the function call.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Its not your full code, so I can't know everything about it...

But try this netherless:

#include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode", 1); Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem = TrayCreateMenu("Settings")
$displayitem = TrayCreateItem("Display", $settingsitem)
$printeritem = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

$start = TimerInit ()

While 1
    $dif = TimerDiff($start)
;If $dif >= 2*1000*60 Then; SHould be 2 minutes if my calculations are correct... Otherwise i'm testing with 5 seconds
    If $dif >= 5000 Then
        _givemsg ()
        $start = TimerInit ()
    EndIf
    $msg = TrayGetMsg()
    Select
        Case $msg = $aboutitem
            MsgBox(64, "About:", "AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Func _givemsg()
    MsgBox(0, "", "msg")
EndFunc  ;==>_givemsg
Link to comment
Share on other sites

You're looping round without taking a break, use Sleep(100) somewhere in the while loop to free up the cpu.

That's what i used when i tried first, but it puts the whole program to sleep. I need it always active so i can right-click the icon in the systray :).

Anyway, the problem was solved.

Many thanks to everyone :lmao:

Link to comment
Share on other sites

  • Moderators

weirddave,

There is no need to use a Sleep when using the GUIGetMsg or TrayGetMsg functions - from the Help file for both functions:

Remarks

This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU.

Adding a Sleep just makes the whole loop very unresponsive - as mexykanu found. :-)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...