Add FileSystemWatcher and Timer to a .Net 2.0 windows service - windows

Anyone see any potential problems adding a FileSystemWatcher and a Timer
into my derived ServiceBase class??
Malcolm

Just be sure to wrap the events for each in a try-catch as they will run on threads from the Thread Pool. If there is an exception during the processing of that event your service may stop running unexpectedly. You should also keep the processing in the FileSystemWatcher Event to a minimum, for instance, just add the file path to a queue and then have one of your timers or Background Worker process the queue.

No. There should be no problems in using these classes in a Windows Service. Both of these classes (not sure which "Timer" class you're using) should work fine in a Service environment.
If you're running into problems, specifying those might help us figure out how to help...

We have FileSystemWatchers and timers in our derived classes. What problems are you anticipating.

I don't think so. I have used System.Threading.Timer in Windows service & its not working. After 2-3 times the code the does not work thought the service is in the running state.
Still trying to figure out the solution to the problem. If any one knows the solution, do let me know. It's driving me crazy :(

Related

How to avoid passing slow Application_Start times to the end users in ASP.NET

I have quite a slow Application_Start due to having a lot of IoC stuff happen at start up.
The problem I'm trying to solve is, how do I avoid passing that start up time to the end user?
Assumptions
My apps are hosted on AppHarbor so I have no access to IIS. However even if I did, my understudying is that it's best practice to let the app pool recycle, so there's no way to avoid having the Application_Start run regularly (I think it's every 20 minutes on AppHarbor).
My idea to solve it
Initially I thought I'd hit it every minute or something, but that seems too brute force and it may not even stop a user from experiencing the slow start up.
My current solution is to handle the Application_End event, and then immediately hit the App so that it starts up again, thus hopefully not impacting any users.
Is there a better way to solve this issue?
Unfortunately, a longer session timeout will not prevent an IIS app pool recycle when you're using InProcess session state.
Have you considered lazy loading (some of) your dependencies? SimpleInjector has documentation on how to do this, which should be adaptable to most other IoCs:
Simple Injector \ Documentation \ How To \ Register Factory Delegates \ Working With Lazy Factories
In my understanding, to prevent the propagation of startup time to users, you should avoid recycling the App Pool, for which you can use IIS App pool timeout settings,these can be tuned through web.config, not just through IIS console. Additionally you can read more of it here on this SO qurestion. You might not need Application_End hacks to achieve this.
Update :
I found another interesting thing that may help you on this, check this IIS Application Initialization Extension that can be used to preload dependencies as soon as worker process starts. It may help you improve customer experience. Check it out.

What processĀ API do I need to hook to track services?

I need to track to a log when a service or application in Windows is started, stopped, and whether it exits successfully or with an error code.
I understand that many services do not log their own start and stop times, or if they exit correctly, so it seems the way to go would have to be inserting a hook into the API that will catch when services/applications request a process space and relinquish it.
My question is what function do I need to hook in order to accomplish this, and is it even possible? I need it to work on Windows XP and 7, both 64-bit.
I think your best bet is to use a device driver. See PsSetCreateProcessNotifyRoutine.
Windows Vista has NotifyServiceStatusChange(), but only for single services. On earlier versions, it's not possible other than polling for changes or watching the event log.
If you're looking for a user-space solution, EnumProcesses() will return a current list. But it won't signal you with changes, you'd have to continually poll it and act on the differences.
If you're watching for a specific application or set of applications, consider assigning them to Job Objects, which are all about allowing you to place limits on processes and manage them externally. I think you could even associate Explorer with a job object, then all tasks launched by the user would be associated with your job object automatically. Something to look into, perhaps.

windows service doesn't stop

I have a problem concerning a windows service that I have implemented. The service does what it has to and logs various information but when I try to stop it, it doesn't seem to stop and keeps logging.
I am not very familiar with services, so if anyone knows why this is happening please tell me.
Edit: I have tested the service on two servers and the weird thing is that on one server it stops normally, but on the other it doesn't. That is what I don't understand.
IN case you have any threads ensure that you exit those threads in your stop routine. Some threads may still be lingering which is why it does not stop.
One possible reason could be because of some remaining thread (as mentioned by 'ckv').
Have you set proper handler using RegisterServiceCtrlHandler function?
Also make sure that the SERVICE_STATUS structure that you use has its dwControlsAccepted set to accept shutdown or stop (by setting SERVICE_ACCEPT_SHUTDOWN & SERVICE_ACCEPT_STOP). and don't forget to update the service status (using SetServiceStatus function) once the service is up and running.

ServiceBase.OnShutdown and event logs in Windows .Net 3.5

I've written a custom service that overrides ServiceBase.OnShutdown().
Unfortunately, when I log to the event log, nothing is written.
My guess is that the Windows event log was shut down before my service.
Is there a way to order service shutdown so that my servce shuts down
before the event logger? I don't want to have to write out to a file.
Pl. advise. Thanks.
You could try to setup a dependency where your service depends on the Event logger, this is mostly done to make them load in the correct order but I assume that might make sure that your service always was stopped first as well.
As can be seen in this Technet article, you'd need to change the DependOnService value either using the Sc.exe tool or the ChangeServiceConfig API.
There is a way, but it is more or less a Reflection-Hack.
I added my solution to an other post: Here
Hope I could help.

How do you prevent a .NET service from timing out while waiting on a dependent service

I have a C# based service that is dependent on the MSMQ service. In some scenarios the MSMQ service takes a long time to start, apparently resulting in a timeout of the C# service. How can I fix this programatically?
Edit: It appears that the bug report I was working on was incorrect, the service does indeed start eventually. I apologize for the confusion
Normally waiting on a queue should be made in a separate thread, which should be started directly in the OnStart Method of the Service.
If you do it this way, the Start-command succeeds and the service will not time out.

Resources