|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
WMI - Windows events
I've read about the Windows (or any operating) system; about how Windows is the central program collecting all events and distributing it to the appropriate applications.
Since I only know J/VB-Script, is there anyway for me to access these events. I'm trying to have my script wake up whenever a new file is created or when a file is deleted. |
|
#2
|
||||||||
|
||||||||
|
There are no WSH (VBScript) events for this directly, but you can monitor file creation and deletion using WMI events.
Monitoring file creation using __InstanceCreationEvent: vb Code:
Monitoring file deletion using __InstanceDeletionEvent: vb Code:
__________________
Click the image if at any point you don't like my decision.Scripting problems? Windows questions? Ask the Windows Guru! Last edited by Nilpo : June 14th, 2008 at 03:10 AM. Reason: Edited to correct code syntax. |
|
#3
|
|||
|
|||
|
Thank You! Just to get this straight, does the fact that the NextEvent() function never returned make this different than continuous looping, which eats up processor space?
Also, where can I get a full listing of the available events? I also want to know when the computer goes into or out of standby mode, so that my script will stop. |
|
#4
|
||||
|
||||
|
The NextEvent method simply waits for a NotificationQuery to return an event notification. It will sit idle while it waits.
The nice thing about WSH is that when a script sits idle it does not eat up any CPU cycles. As for a complete list of events, you should consult the MSDN library or download a WMI explorer. I'll see if I can find a way to monitor system state changes for you. |
|
#5
|
|||
|
|||
|
Thank you very much!
One more thing though. Now that you bring up the waiting for a function to finish, is there a way to run a function and move on without waiting for a return? Using "void func()", doesn't do the job, although it has no reason to wait. I was trying to have my script running until the computer goes into standby. It's no use if all it does is wait for the standby. My last resort would be to run a separate page with wShell.run and not have it wait. |
|
#6
|
||||
|
||||
|
Quote:
|
|
#7
|
|||
|
|||
|
Why do you need to loop in the first place, if the script is anyhow waiting for the function to return?
Anyhow, this doesn't help me since I am trying to run the script until the computer goes into standby. I need the script to go on doing other things while listening for that event. Is it possible at all for two functions to run at the same time? |
|
#8
|
||||
|
||||
|
Quote:
Quote:
|
|
#9
|
||||
|
||||
|
In VBScript, events are handled asynchronously. This means that a script will continue to execute in the background while it waits for an event to occur. You can effectively have a script perform two operations at the same time: one set of instructions are carried out upon execution, and a second set of instructions are performed in response to any events. This can also be accomplished when using WMI events. The examples that I've provided above use traditional WMI events. These events are carried out synchronously (or semisynchronously depending on the event) meaning that the script will site idle while waiting for events and cannot carry out any other instructions. To overcome this, we can use the same notification query that we've used above, but instead, use a method that carries out the query asynchronously. Let's look at how the code changes.The code begins in the same manner by connecting the the WMI Service object.Next, we need to create a sink object. This is the object that will be returned by the asynchronous event monitor. It is provided by the WbemScripting object. (WMI's scripting interface.) You'll notice a slight variation in this declaration. Notice that I'm using the WScript object's CreateObject method rather than VBScript's. This allows me to specify an additional parameter called prefix. You'll see how we use this shortly. |