Click to See Complete Forum and Search --> : Getting an application out of sleep.


Boumxyz2
September 10th, 2001, 09:41 AM
Hi !

I would like to be able to do this in VB and I want to know if it's possible to do it. Ok I'll explain how it will work.

Ok let's say the application is sleeping ( mostly it will be in sleep mode ). Then when a specific file appears in a set directory, the application gets out of sleep mode and then it start processing the file. When it is done it returns to sleep mode.

I'm guessing that I need the OS telling my application if the file exists and I would prefer to keep the application in sleep mode and not checking in the directory every minutes for a new files. Also if it's possible will I need to run the application as a service ?

Any help is welcome.

Thanks in advance

Nicolas

John G Duffy
September 10th, 2001, 10:15 AM
Haven't tried it, but supposedly this little sample will wake up yopur program whenever a Directory fiel has been changed.
Hope it helps

'The FindFirstChangeNotification function creates a change notification handle
'and sets up initial change notification filter conditions.
'A wait on a notification handle
'succeeds when a change matching the filter
'conditions occurs in the specified directory or subtree.
private Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
private Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
private Const FILE_NOTIFY_CHANGE_SIZE = &H8
private Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
private Const FILE_NOTIFY_CHANGE_SECURITY = &H100
private Const FILE_NOTIFY_CHANGE_ALL = &H4 Or &H2 Or &H1 Or &H8 Or &H10 Or &H100
private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (byval lpPathName as string, byval bWatchSubtree as Long, byval dwNotifyFilter as Long) as Long
private Declare Function FindCloseChangeNotification Lib "kernel32" (byval hChangeHandle as Long) as Long
private Declare Function FindNextChangeNotification Lib "kernel32" (byval hChangeHandle as Long) as Long
private Declare Function WaitForSingleObject Lib "kernel32" (byval hHandle as Long, byval dwMilliseconds as Long) as Long
private Declare Function ResetEvent Lib "kernel32" (byval hEvent as Long) as Long
private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim Ret as Long
'set the notification hook
Ret = FindFirstChangeNotification("C:\", &HFFFFFFFF, FILE_NOTIFY_CHANGE_ALL)
'Wait until the event is triggered
WaitForSingleObject Ret, &HFFFFFFFF
MsgBox "Event Triggered for the first time"
'Reactivate our hook
FindNextChangeNotification Ret
'Wait until the event is triggered
WaitForSingleObject Ret, &HFFFFFFFF
MsgBox "Event Triggered for the second time"
'Remove our hook
FindCloseChangeNotification Ret
End Sub



Here are some comments that apply to the various parameters
'
· lpPathName
[in] Pointer to a null-terminated string that specifies the path of the directory to watch.
Windows NT/2000: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions.
Windows 95/98: This string must not exceed MAX_PATH characters.

· bWatchSubtree
[in] Specifies whether the function will monitor the directory or the directory tree. If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.

· dwNotifyFilter
[in] Specifies the filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values. Value Meaning
FILE_NOTIFY_CHANGE_FILE_NAME Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file name.
FILE_NOTIFY_CHANGE_DIR_NAME Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory.
FILE_NOTIFY_CHANGE_ATTRIBUTES Any attribute change in the watched directory or subtree causes a change notification wait operation to return.
FILE_NOTIFY_CHANGE_SIZE Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_LAST_WRITE Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.
FILE_NOTIFY_CHANGE_SECURITY Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return.

John G

Boumxyz2
September 10th, 2001, 10:35 AM
Thanks.

I need to know if it's using CPU time while in sleep mode. Because that's actually the problem we're concerned about. Reducing the amount of times it is checking for a new file in the directory is also an amelioration we're trying to do.

Thanks for the help I'll check further. Not used to VB much yet.

Nicolas

enigmaos
September 10th, 2001, 10:42 AM
[vbcoe]
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim oFolder As Object
Dim oFile As Object

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Dir1.Path)

Do While oFolder.Files.Count < 0
Sleep (30000) ' 30000 milleseconds = 30 seconds
Loop

For Each oFile In oFolder.Files
' Process file
Next oFile

[/vbcode]


enigmaos@yahoo.com

Boumxyz2
September 10th, 2001, 10:48 AM
Hum thanks but it's not the way I would like to do. You suggest to check every file when I actually want to sleep all the time and be awaken by windows that tells me HEy there's a new file process it.

Thanks

Nicolas

John G Duffy
September 10th, 2001, 02:19 PM
I can't answer your question regarding "Sleep" mode. The WaitFOrSingleObject is what suspends your app until a change occurs. As to whether your App is put in actual "Sleep" mode, I cant say.

John G

Boumxyz2
September 10th, 2001, 02:41 PM
Actually it works I had to correct some of the code. Thanks a lot.

Pass false instead of &HFFFFFFFF for the FindFirstNotationChange second parameter and everything works. Else it won't wait.

Nicolas

Thanks again

nolc
March 1st, 2002, 11:16 AM
Hey Nicolas,
Did you ever figure out how to jump out of sleep mode when there was a change in a specified directory? If you did...can you help me out?!! I'm trying to figure out the same thing....

Thanks

No L c
VB Developer

Boumxyz2
March 3rd, 2002, 11:33 PM
This code here works good or the example on www.allapi.net works too

http://63.236.73.79/cgi-bin/bbs/wt/showpost.pl?Board=vb&Number=61206&page=&view=&sb=

Nicolas Bohemier
______________

Un sourire ne coûte rien, mais il rapporte beaucoup; il enrichit celui qui le reçoit sans appauvrir celui qui le donne.

Frank Irving Fletcher