Jump to content
Sign in to follow this  
bobchernow

Context Menu using OnEvent mode

Recommended Posts

bobchernow

OK, I have searched and cannot find the answer to what should be a simple question. Can I use context menus items on listview items when using onevent mode? I am using the standard listview items, not the UDF. I need to use onevent mode due to the nature of the code. It becomes non responsive in polling mode.

The code is still half baked but what it does is basically issue the Net Sessions command on an XP machine and if the any session is IDLE for a long time and has no open files, delete that session. I use my XP box as a file server and at times hit the 10 maximum open sessions. This program runs on the box and keeps it clean. I now want to add the abiity to right-click a session and delete it manually.

For some reason, even though I can bring up a context menu, I can not figure out how to get an item to fire an event.

Bob

CODE

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <ListviewConstants.au3>

#include <NetShare.au3>

#include <Date.au3>

Opt('MustDeclareVars', 1)

Opt("GUIOnEventMode", 1)

HotKeySet("^!q", "Terminate")

Global $listview, $aItems[100][3], $iI

GUINetShareDeleteAuto()

Func GUINetShareDeleteAuto()

Local $msg

GUICreate("Sessions", 780, 360, 100, 200, -1)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUISetBkColor(0x00E0FFFF) ; will change background color

$listview = GUICtrlCreateListView("Computer |User Name |Client Type|Opens |Active seconds |Idle seconds", 10, 10, 760, 340, -1, $LVS_EX_FULLROWSELECT);,$LVS_SORTDESCENDING)

GUICtrlSetOnEvent($listview, "ListView")

GUISetState()

While 1

Populate()

Sleep(10000)

WEnd

EndFunc ;==>GUINetShareDeleteAuto

Func Populate()

Local $aInfo, $rc, $hFile

For $iI = 1 To $aItems[0][0]

GUICtrlDelete($aItems[$iI][0])

Next

$aInfo = _Net_Share_SessionEnum()

For $iI = 1 To $aInfo[0][0]

$aItems[0][0] = $aInfo[0][0]

$aItems[$iI][0] = GUICtrlCreateListViewItem($aInfo[$iI][0] & "|" & $aInfo[$iI][1] & "|" & $aInfo[$iI][6] & "|" & $aInfo[$iI][2] & "|" & $aInfo[$iI][3] & "|" & $aInfo[$iI][4], $listview)

;GUISetOnEvent($GUI_EVENT_SECONDARYDOWN, "ItemClicked")

$aItems[$iI][1] = GUICtrlCreateContextMenu($aItems[$iI][0])

$aItems[$iI][2] = GUICtrlCreateMenuItem("About item", $aItems[$iI][1])

GUISetOnEvent($aItems[$iI][2], "ItemClicked")

If $aInfo[$iI][4] >= 900 Then

If $aInfo[$iI][2] = 0 Then

_Net_Share_SessionDel("", $aInfo[$iI][0])

$hFile = FileOpen(@ScriptDir & "\NetShareDelete.log", 1)

FileWriteLine($hFile, _Now() & ": Session " & $aInfo[$iI][0] & " with " & $aInfo[$iI][2] & " files open was deleted.")

FileClose($hFile)

EndIf

EndIf

Next

Sleep(5000)

EndFunc ;==>Populate

Func Terminate()

Exit (0)

EndFunc ;==>Terminate

Func CLOSEClicked()

Exit

EndFunc ;==>CLOSEClicked

Func ListView()

MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)

EndFunc ;==>ListView

Func ItemClicked()

MsgBox(0, "Item Clicked", "clicked=" & $aItems[$iI][0], 2)

EndFunc ;==>ItemClicked


--------------------bobchernow, Bob ChernowWhat a long strange trip it's beenUDFs: [post="635594"]Multiple Monitor Screen Resolution Change[/post]

Share this post


Link to post
Share on other sites
Danny35d

In your Populate() function you are using GUISetOnEvent($aItems[$iI][2], "ItemClicked") instead of GUICtrlSetOnEvent($aItems[$iI][2], "ItemClicked")


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites
bobchernow

In your Populate() function you are using GUISetOnEvent($aItems[$iI][2], "ItemClicked") instead of GUICtrlSetOnEvent($aItems[$iI][2], "ItemClicked")

Thank you. I figured it was something dumb. Good eyes as I coulda looked and looked and never saw that.

Bob


--------------------bobchernow, Bob ChernowWhat a long strange trip it's beenUDFs: [post="635594"]Multiple Monitor Screen Resolution Change[/post]

Share this post


Link to post
Share on other sites
bobchernow

I need help again. I have all the code working nicely. I can get the context menus and get them to work fine. Since the program runs continuously, I am creating and destroying the context menus and items as well. This all works for several (12+) hours with no problems. I even am using msgbox to display the deletions. Then after a period of time, the Msgbox's stop displaying, and the context menus stop working. I suspect resources are getting used up or something. Any ideas of what I am doing stupid.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
#include <NetShare.au3>
#include <Date.au3>


Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)
HotKeySet("^!q", "Terminate")
Global $listview, $aItems[100][10], $iI, $bDeleted
Const $sComputerName = 0, $sUserName = 1, $sClientType = 6, $sOpens = 2, $sActiveSeconds = 3, $sIdleSeconds = 4
Const $iListViewItem = 0, $iContextMenu = 1, $iContextMenuItem = 2

GUINetShareDeleteAuto()

Func GUINetShareDeleteAuto()

    GUICreate("Sessions", 780, 360, 100, 200, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUISetBkColor(0x00E0FFFF); will change background color
    $listview = GUICtrlCreateListView("Computer         |User Name       |Client Type|Opens |Active seconds |Idle seconds", 10, 10, 760, 340, -1, $LVS_EX_FULLROWSELECT);,$LVS_SORTDESCENDING)
    GUICtrlSetOnEvent($listview, "ListView")
    GUISetState()

    While 1
        Populate()
        Sleep(4000)
    WEnd

EndFunc  ;==>GUINetShareDeleteAuto

Func Populate()
    Local $aInfo, $rc, $hFile, $jJ

; Kill any previous controls
    For $iI = 1 To $aItems[0][0]
        MsgBox(0,"LVI","delete of ctrl " & $aItems[$iI][$iListViewItem] & " = " & GUICtrlDelete($aItems[$iI][$iListViewItem]),1)
        MsgBox(0,"CM","delete of ctrl " & $aItems[$iI][$iContextMenu] & " = " & GUICtrlDelete($aItems[$iI][$iContextMenu]),1)
        MsgBox(0,"CMI","delete of ctrl " & $aItems[$iI][$iContextMenuItem] & " = " & GUICtrlDelete($aItems[$iI][$iContextMenuItem]),1)
    ;GUICtrlDelete($aItems[$iI][$iListViewItem])
    ;GUICtrlDelete($aItems[$iI][$iContextMenu])
    ;GUICtrlDelete($aItems[$iI][$iContextMenuItem])

    Next

; Enumerate all sessions
    $aInfo = _Net_Share_SessionEnum()

; For all active sessions
    For $iI = 1 To $aInfo[0][0]
        $bDeleted = 0
    ; Check to see if this session needs to be deleted
        If $aInfo[$iI][$sIdleSeconds] >= 900 Then
            If $aInfo[$iI][$sOpens] = 0 Then
                _Net_Share_SessionDel("\\bobchernow", $aInfo[$iI][$sComputerName])
                $hFile = FileOpen(@ScriptDir & "\NetShareDelete.log", 1)
                FileWriteLine($hFile, _Now() & ": Session " & $aInfo[$iI][$sComputerName] & " with " & $aInfo[$iI][$sOpens] & " files open was deleted.")
                FileClose($hFile)
                $bDeleted = 1
            EndIf
        EndIf

    ; If the session was not deleted then build the context menu
        If $bDeleted = 0 Then
            $aItems[0][0] = $aInfo[0][0]
            $aItems[$iI][$iListViewItem] = GUICtrlCreateListViewItem($aInfo[$iI][$sComputerName] & "|" & $aInfo[$iI][$sUserName] & "|" & $aInfo[$iI][$sClientType] & "|" & $aInfo[$iI][$sOpens] & "|" & $aInfo[$iI][$sActiveSeconds] & "|" & $aInfo[$iI][$sIdleSeconds], $listview)
            $aItems[$iI][$iContextMenu] = GUICtrlCreateContextMenu($aItems[$iI][$iListViewItem])
            $aItems[$iI][$iContextMenuItem] = GUICtrlCreateMenuItem("Delete Session", $aItems[$iI][$iContextMenu])
            GUICtrlSetOnEvent($aItems[$iI][$iContextMenuItem], "ItemClicked")
            For $jJ = 0 To 6
                $aItems[$iI][$jJ + 3] = $aInfo[$iI][$jJ]
            Next
        EndIf
    Next

;Sleep(5000)
EndFunc  ;==>Populate


Func Terminate()
    Exit (0)
EndFunc  ;==>Terminate

Func CLOSEClicked()
    Exit
EndFunc  ;==>CLOSEClicked

Func ListView()
    MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
EndFunc  ;==>ListView

Func ItemClicked()
    Local $i, $sSessionName, $rc

    For $i = 1 To $aItems[0][0]
    ; If we find the item that caused this event, delete the session
        If $aItems[$i][$iContextMenuItem] = @GUI_CtrlId Then
            $sSessionName = "\\" & $aItems[$i][3]; SessionName
            $rc = _Net_Share_SessionDel("\\bobchernow", $sSessionName)
            Populate()
            ExitLoop
        EndIf
    Next
EndFunc  ;==>ItemClicked

Bob


--------------------bobchernow, Bob ChernowWhat a long strange trip it's beenUDFs: [post="635594"]Multiple Monitor Screen Resolution Change[/post]

Share this post


Link to post
Share on other sites
qsek

I suspect resources are getting used up or something.

I suggest you to check memory usage of your script in the taskmanager.

If its a memory leak then maybe GUICtrlDelete doesnt delete the hooked GUICtrlOnEvent on that listviewitem, so it stays in memory.

Edited by qsek

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

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  

×