FileSystemWatcher: Created event didn't fire on file creation - events

I have one Window service, which is watching (Subscribing) a shared location on some other server as below (Using FileSystemWatcher):
_fileSystemWatcher = new FileSystemWatcher(filePath);
_fileSystemWatcher.Created += FileSystemWatcher_Created;
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher.EnableRaisingEvents = true;
However, when new file was received and copied onto location, Created event didn't fire. When I restarted my service and copied file, event fired successfully.
I am trying to find answer, why event didn't fire earlier. Is it possible that if after initializing FileSystemWatcher, this is some network outage, so events wont be fired and when network is up, FileSystemWatcher needs to be reinitialized, to fire respective events?
Also I see that there was one more service instance which was watching same share location, whose event fired successfully. Just FYI: Second service instance was started recently (Possibly network outage was during weekend).

Related

Windows GlobalSystemMediaTransportControlsSession Events Not Fired in Service

I'm trying to listen to events dispatched by GlobalSystemMediaTransportControlsSession in a Windows Service (in Rust).
I made a reproduction repository with instructions.
The most important part is this:
session.PlaybackInfoChanged(TypedEventHandler::new(move |_, _| {
tx.send(()).ok();
Ok(())
}))
Here a new event handler is created that listens on every open application that emits SMTC metadata for changes to the playback, i.e. play/pause/stop events.
Once the event is fired, the application terminates (rx receives an event).
This works fine in a "regular" executable (e.g. by running cargo run).
However, once I'm running this as a service (through nssm*), no event is emitted (the log-file is not created/written to).
I couldn't find any documentation by Microsoft related to services and events. Is there a workaround, am I doing something wrong or is this known to not be supported?
(*) I'm using nssm here, but the same happens when I'm running the executable as a service. This however would include unnecessary code for managing the service.
So I'd guess the problem is that the executable doesn't run with the user-account.
Then I'm wondering why I can get the sessions, and even metadata, in the first place and no error is thrown/emitted.

MediaFoundation: cleaning up after Media Sources

Ok, so I get the gist of MediaFoundation:
When I start my App I create an IMFMediaSession.
When I want to play a file I create an IMFMediaSource, create the necessary IMFTopology, and I set it to the IMFMediaSession. Depending on the flag used (dwSetTopologyFlags) the new media source will either play immediately or just get added to a queue to be played by the MediaSession when the existing topologies/mediaSources are done playing.
Now my issue is cleaning up after old media sources.
Is there an event that is sent by the MediaSession right before the topology used is about to be removed? That way I could access the media source from there and call ShutDown on it (thus avoiding memory leaks) without having to keep references to previous mediaSessions myself in my code.
//=============================================================================== Update:
According to MSDN:http://msdn.microsoft.com/en-us/library/windows/desktop/aa372153%28v=vs.85%29.aspx
I need to call ShutDown on any IMFMediaSource I create, before releasing it.
With this in-mind, mixing MFSESSION_SETTOPOLOGY_FLAGS (immediate/clear_current with no flags) can cause a memory-leak, as the Session removes topologies from its queue without my application having the chance to call shutdown on their sources.
Also, if I call Shutdown on the current mediaSource before the next mediaSource's topology has been set (i.e. before I get the MESessionTopologyStatus event) then I can't playback the next mediaSource. Why? I don't know, msdn does not explain what resources are released when I call shudown on a mediaSource, so I guess something I still need in order to play the next mediaSource is being released...
My experience is that if you do not call Shutdown on Source, you will have memory leak. Check my project MFNode. There is a custom MediaSession (MFNodePlayer project).
If you uncomment the call to Shutdown on the source, you will see memory leak.
A lot of source expect you call Shutdown because you need to wait stop message from the source (they are usually asynchronous).
If you try to use the the source before it stops, you will face some strange error/behaviour and memory leak.
You have to wait the source to stop, in order to have a stable application. And calling Shutdown on source is here for that.

SSIS WMI System Event Watcher

I am currently working with a WMI System Event Watcher in a control flow task that is tied to another sequence container. The WMI system event watcher fires just fine, but only does it once. Is there a way I can get it to reset after a successful run? I have tried setting the watcher to "Watch for the event again" and I've even put the event watcher into a for loop with an infinite loop, but again the problem there is, the watcher will keep running and nothing else happens. Any suggestions would be appreciated.

Windows OS level file open event trigger

Colleagues, I have need to run a script/program on certain basic OS level events. In particular when a file in Windows is opened. The open may be read-only or to edit, and may be initiated by a number of means, either from windows explorer (open or ), be selected from a viewing or editing application from the native file chooser, or drag-n-drop into an editing or viewing application.
Further, i need the trigger to "hold" the event from completing the action until the runtime on the program has completed. The event handler program may return a pass state, or fail state. If fail state has been returned, then the event must disallow the initially requested action.
Lastly, I need to add to the file in question a property or attribute that will contain metadata that will be used by the above event trigger handler program to make a determination as to the pass/fail condition that will ultimately determine if the user is permitted to open the file.
Please note that this is NOT a windows event log situation, but one at the OS level file open event.
thanks very much for your help.
Edit
What I had hoped that someone was aware of was an OS level trigger, similar to how you can enable a trigger on an event-log writer occurance in Windows (ie: send an email, run a script when an event hits the event logger).
The application environment I work in, a PLM system, allows for event handling. So, when a user initiates an action, say a checkout of a file from an object, there are 3 available handlers or triggers that can be programmed. The "check" or "pre-action" trigger is a process that fires when the user starts initiates the event, but before the system permits it to be processed. So, one can block or otherwise alter the response of the system programmatically based on some condition, like user context.
I have also enabled startup/shutdown script firing via group policy, so i can delay a server shutdown until a database has closed properly. This was done in Windows server 2003. So, with that hook, i could manage shutdown or startup.
Hence, I had hoped someone might be aware of an OS trigger that would do the same for a file-open operation. This would be deployed on all workstations and desktops.
Again, if there are any ideas, it would be greatly appreciated.
Thanks again.
regards,
j
A couple strategies come to mind. One is to run the app with your own DLL to intercept API calls like WriteFile. Another would be to run the app under the debugger interface.

Detecting windows shutdown event

I have a windows Service always running once PC started, how would i detect using .NET 3.5 or WMI, when the user or some other application/process has requested a shutdown or reboot in windows.
I do not want to stop it, just log the time and who initiated the shutdown (user or process is fine, don't really need to know which process)
Thank you
Use the Microsoft.Win32.SystemEvents.SessionEnding event.
However, note that you are in a Windows Service, and this event won't fire for you unless you create a hidden form in your service. You can get one of these for free by enabling the "Interact with Desktop" option on the service.
You're looking for the SystemEvents.SessionEnding event.
Occurs when the user is trying to log
off or shut down the system.
Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.

Resources