Jump to content
Sign in to follow this  
mexykanu

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

Recommended Posts

mexykanu

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.

Share this post


Link to post
Share on other sites
BrettF
mexykanu

#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.

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
BrettF

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

Share this post


Link to post
Share on other sites
mexykanu

Thank you melba and brettf, that worked although i don't know why my code used the processor at 100%.

Edited by mexykanu

Share this post


Link to post
Share on other sites
weirddave

Thank you melba and brettf, that worked although i don't know why my code used the processor at 100%.

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

Share this post


Link to post
Share on other sites
mexykanu

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:

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
weirddave

Interesting, I must admit, I'm not too familiar with GUI and Tray functions. I find a small sleep does often help stop loops hogging the cpu tho :) even if it's just 1ms to keep it nice and responsive. Obviously not much help in this case....

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.