Net windows service thread
WindowsServices that let us host our. NET , as a Windows Service. It automatically provides logging capabilities to the Windows Events , the default output where Windows Services should log information to, as well as automatically logging life-cycle events , such as Started , Stopping and Stopped events.
It also provides a helpful method to detect if your process is running as a windows service or not. Lets start with the code we ended up with at our last blog post , and add the Microsoft. WindowsServices NuGet package :. With this NuGet installed inside our project, we can change our Main method to initialize a Host, which will initialize it as a Windows Service and run it. The code itself is simpler than it sounds:.
We need both, since we want the parsed one and due to the fact that the Host. CreateDefaultBuilder method allows you to pass parameters from the command line to configure it, so we need to pass down the original string array to it. We start by creating this default builder. We then configure its logging to filter only if the log level is Information or higher, and then we add our hosted service. We also create a singleton with our parsed CommandLineOptions , which allows us to use it inside the ImageClassifierWorker class that is initialized for us by the default builder.
We will expect a CommandLineOptions parameter in the constructor of our ImageClassifierWorker which will be automatically injected for us. These parameters let us choose where the Events from our logs will be stored. Last, but not least, we are initializing this host as a Windows Service. This last step is as easy as calling the UseWindowsService method, which configures the host to use the logging properties we just set, as well as setting up automatic logging for the Windows Service lifetime events.
Since this new code will listen to new files in the directory we specify, we need a way to filter the file extensions we want to allow processing. We could hard-code this, but lets simply add a parameter to our CommandLineOptions class, which will easily handle all the parsing for us:. See how easy it is to support our list of extensions?
Since these are the most commons ones, we support png , jpg , and jpeg , and all the classes we use to load our image handle these extensions, so we are safe with them. Now that you understand the structure of our Program. This is the class that will run our WinML code.
The ExecuteAsync method is going to be called automatically for us, so we need a mechanism to keep it from returning, to keep our process from ending. There are many ways to achieve that, specially since we need to return a Task. This should be at the very end of our ExecuteAsync method. The stoppingToken is passed down to our ExecuteAsync method, and it is an instance of CancellationToken which will be cancelled when our service is requested to stop. Remember that this code is headless no user interface at all , so we need a way to know what our code is executing, and that is done through logs.
At the start of our method, we should load our WinML model, pretty much the same way we were doing before:. This creates a FileSystemWatcher instance that will hook up to some Windows events for us, and automatically raise the Created event whenever there is a new file created on the folder we are watching. Notice that we are adding Filters to it, which are mapping to the extension filters coming from our new command line argument.
We are waiting milliseconds 1 second , before we process the file. The FileSystemWatcher is attached to the Windows events, and is so fast that it will raise the Created event even before the file is closed by whichever process is still creating it. We could have created a retry logic if we caught this specific exception but, again, this solution is a simplification, and will keep our code simple for this sample.
The ProcessFileAsync method expects a full path to an image file and a confidence float, just like we had before. Friday, 27 February Multi threaded windows service. We recently had a requirement to host an internal windows service which would continuously process small, but relatively long for example seconds running packets of work obtained from a queue.
We also wanted a degree of parallelism for efficiency, the service being hosted on a relatively powerful multi-core windows server. Each packet of work would represent a business critical process - so we were keen to ensure that the design of the service was robust and that stopping the service for any reason would not cause worker threads to be terminated abruptly and in addition there was sufficient scope for error handling and compensation within the service.
We did not have a requirement to cluster an individual service instance. It quickly became apparent that we needed to carefully consider how the windows Service-Control-Manager interacted with our service. What started off as a relatively trivial bit of code ended up being slightly more complex. I couldn't find very many good examples, so here's an idea of how the orchestrating service start and stop logic could be implemented, should it be useful.
Note: This is an example and not production quality code. All comments welcomed. Starting The first goal with this example is to ensure that the service OnStart method clears quickly it isn't blocked That we start a "foreground thread" to manage the background threads.
Foreground threads are less likely to be abruptly terminated by windows. Ankit Shah Ankit Shah 71 1 1 gold badge 1 1 silver badge 2 2 bronze badges. This question is probably better for SO — softveda. Pratik - I'm not so sure about that. There's not a specific coding issue. It's a design question which is on topic for here. His requirements cannot be meet. There are very few computers on the market that the capability to run 12 threads concurently.
His design requirements are not realistic. Ramhound: calling ThreadPool. GetMaxThreads on my year old i3 desktop reports worker threads and completion port threads Add a comment.
Active Oldest Votes. Improve this answer. Homde Homde Steven A. Lowe Steven A. Lowe With the advent of TPL. NET 4 onwards — Sudhanshu Mishra. The SafeThread class is a great idea! Quick query: With the advent of TPL.
NET 4 onwards and thus the Task construct, I believe in most situations Task is the way to go rather than creating Threads - what do you think? NET in a while; perhaps this will help stackoverflow.
0コメント