 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
IN2ITive
Joined: 15 Apr 2008 Posts: 47
|
Posted: Fri May 30, 2008 7:47 pm Post subject: Issue With GUI not Responding/Monitoring Folder Issue |
|
|
Hi:
This script simply monitors a folder for new files. If a new file is detected then its contents are sent to a server for processing. That functionality works fine. The problem I am having is that the GUI does not respond to user actions while the script is running, obviously due to the fact that the monitoring process is running.
I believe that this is a "need to thread" issue, but I have no idea where I need to do this, or how.
A similar script resides here...FolderSpy.
http://www.autohotkey.com/forum/viewtopic.php?t=22862&highlight=folderspy
My code has still artifacts from testing and trying to get other things to work as well. But my main problem I need fixing is the GUI not responding.
Note that the script utilizes an INI file and also is based on vbscript embedding.
Any help would be greatly appreciated.
| Code: |
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
; Author: A.N.Other <myemail@nowhere.com>
;
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
#INCLUDE WindowsScript4AHK.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
objTcp =
objConstants =
strComputer =
objWMIService =
objFSO =
objFolder =
contents =
strContents =
mycode =
CurrentItem =
DateCreated =
FileSize =
FileSizeInKB =
intNumberOfFilesAfter =
intNumberOfFilesBefore =
blnDoOnce =
ListInsertionNeeded =
TextToEdit =
intNumberOfFiles =
intTotalFileSizeKB =
ServerName =
ServerPort =
IniRead, ListenFolder, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.ini, Setup, Listen_To_Folder
IniRead, ServerName, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.ini, Server, Name
IniRead, ServerPort, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.ini, Server, Port
Gui, Font, S10 CDefault, Verdana
Gui, Add, Text, x16 y17 w120 h20 , Listening Folder:
Gui, Add, Edit, x186 y17 w900 h30 +ReadOnly vListenFolder, % ListenFolder
Gui, Add, GroupBox, x6 y57 w1090 h270 , New Files Detected:
Gui, Add, Text, x16 y277 w120 h30 , New Files Count:
Gui, Add, Edit, x186 y277 w110 h30 vFileCount,
Gui, Add, GroupBox, x6 y337 w1090 h470 , New Files Content:
Gui, Add, ListView, x16 y87 w1070 h180 +Grid vSpyLV, Time|File/Folder Name|Size (KB)
Gui, Add, Button, x505 y817 w100 h30 , Close
Gui, Add, Edit, x16 y367 w1070 h380 +ReadOnly,
Gui, Add, Text, x16 y757 w170 h30 , Total KB Sent To Server:
Gui, Add, Edit, x196 y757 w130 h30 ,
; Generated using SmartGUI Creator 4.0
Gui, Show, x50 y10 h900 w1113, HL7 Auto Route
LV_ModifyCol( 1, "170" )
LV_ModifyCol( 2, "790" )
LV_ModifyCol( 3, "100 Integer" )
Gui, Add, StatusBar
SB_SetParts( 555, 555 )
;Return
WS_Initialize("VBScript")
initcode=
(
Set objTcp = CreateObject("ActiveXperts.Tcp")
Set objConstants = CreateObject("ActiveXperts.ASConstants")
objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
'objTcp.Connect ServerName, ServerPort
objTcp.Connect "servername", 6008
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'If blnDoOnce = False Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("c:\TestListener")
'intNumberOfFilesBefore = objFolder.Files.Count
'MsgBox("Before:" & intNumberOfFilesBefore)
'End If
)
ok := WS_Exec(initcode)
If (!ok)
{
Msgbox Error:`n%ErrorLevel%
}
Else
{
SB_SetText( " CONNECTION ESTABLISHED ")
}
mycode=
(
'blnDoOnce = True
ListInsertionNeeded = False
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\TestListener""'")
'intNumberOfFilesAfter = objFolder.Files.Count
'MsgBox("After:" & intNumberOfFilesAfter)
'MsgBox("Here")
'Do While intNumberOfFilesAfter > intNumberOfFilesBefore
'MsgBox("There")
Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
strReturned = objEventObject.TargetInstance.PartComponent
CurrentItem = Split(strReturned, "CIM_DataFile.Name=")(1)
CurrentItem = Replace(CurrentItem, """", "")
CurrentItem = Replace(CurrentItem, "\\", "\")
DateCreated = Date & " " & Time
'MsgBox(DateCreated)
Set filetxt = objFSO.OpenTextFile(CStr(CurrentItem), 1, False)
strContents = filetxt.ReadAll
Set objFile = objFSO.GetFile(CurrentItem)
FileSize = objFile.Size
FileSizeInKB = FileSize / 1024
filetxt.Close
contents = Chr(11) & strContents & Chr(28) & Chr(13)
For i = 1 To Len(contents)
objTcp.SendByte Asc(mid(contents, i, 1))
Next
'intNumberOfFilesBefore = intNumberOfFilesBefore + 1
'MsgBox("Before (AfterLoop):" & intNumberOfFilesBefore)
ListInsertionNeeded = True
intNumberOfFiles = intNumberOfFiles + 1
intTotalFileSize = intTotalFileSize + FileSize
intTotalFileSizeKB = intTotalFileSize / 1024
End Select
'Loop
)
Settimer, AddToGUI, 500
Return
AddToGUI:
ok := WS_Exec(mycode)
If (!ok)
Msgbox Error:`n%ErrorLevel%
WS_Eval(strComputer, "strComputer")
WS_Eval(contents, "contents")
WS_Eval(strContents, "strContents")
WS_Eval(CurrentItem, "CurrentItem")
WS_Eval(DateCreated, "DateCreated")
WS_Eval(DateCreated, "DateCreated")
WS_Eval(intNumberOfFiles, "intNumberOfFiles")
WS_Eval(intTotalFileSizeKB, "intTotalFileSizeKB")
WS_Eval(FileSizeInKB, "FileSizeInKB")
FormatTime, TStamp, DateCreated, yyyy-MM-dd HH:mm:ss
LV_Add("", TStamp, CurrentItem, FileSizeInKB)
GuiControlGet, TextAlreadyThere,, Edit3
GuiControl,, Edit3, %TextAlreadyThere%`n`n%strContents%-----------------------------------------------------------------------------------------------------------------------------------------------------
GuiControl, , Edit2, %intNumberOfFiles%
GuiControl, , Edit4, %intTotalFileSizeKB%
Return
WS_Exec("objTcp.Disconnect")
WS_Uninitialize()
ButtonClose:
ExitApp |
|
|
| Back to top |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 903 Location: The Interwebs
|
Posted: Sat May 31, 2008 8:23 am Post subject: |
|
|
| Possibly use of "Critical" command could help here? |
|
| Back to top |
|
 |
IN2ITive
Joined: 15 Apr 2008 Posts: 47
|
Posted: Sat May 31, 2008 6:32 pm Post subject: |
|
|
Thanks Krogdor...didn't think of that but after reading about Critical I don't know if it will help me (probably because I have a lack of experience with threading).
I don't know if I should create a critical thread at the beginning of my script, and then before executing the vbscript code create another thread that is not critical, and delete that thread when the vbscript code is finished. Since the execution of the vbscript code with be constant, almost every second or so, I don't know the impact of creating/deleting threads that often.
If you or anyone else has any ideas or an example of how critical or threading would help me out please let me know.
Thanks. |
|
| Back to top |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 903 Location: The Interwebs
|
Posted: Sat May 31, 2008 6:48 pm Post subject: |
|
|
| Well, since the VBScript thread is under the AddtoGui label, you could leave that thread as normal and make the part of the thread containing the GUI to critical status? I've never had a need for critical, so I don't really know. |
|
| Back to top |
|
 |
IN2ITive
Joined: 15 Apr 2008 Posts: 47
|
Posted: Sat May 31, 2008 7:08 pm Post subject: |
|
|
OK, I think I got what you are saying Krogdor. Thanks for being patient. I also re-read the FolderSpy thread and it is even pointed out in the first post that:
| Quote: | The problem with synchronous mode in AHK is that AHK is single threaded and ReadDirectoryChangesW can halt/freeze every other operation when it is waiting for an event in the watched folder. Halt means: you cannot start an another thread via hotkey /settimer .. the GUI ( if any ) would not respond for any messages ( including mouse ).. not even a right click on tray icon would work!
To overcome the said effect, the script calls ReadDirectoryChangesW in a seperate thread using CreateThread() and RegisterCallback().
When called in synchronous mode, ReadDirectoryChangesW sets the variable nReadLen ( with number of bytes ) when it exits the wait state.
Therefore, the script calls ReadDirectoryChangesW in a seperate thread and loops to check the variable nReadLen to ascertain whether the wait state has ended.
|
While I am not directly using ReadDirectoryChangesW the result is the same.
Will let you know how it goes in a little while.
Thanks again. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|