Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle application close events using C#
    primarykey
    data
    text
    <p>I have an windows service that monitors a folder for new files being created.when a file gets added to the folder, the service will send the file for processing to a 2nd application. The 2nd app is actually a console app but is set as 'Windows Application' to avoid creating of the console window. The service and the 2nd app both run on a server. To implement only a single file to process each time, a semaphore has been implemented in the 2nd app, like so:</p> <pre><code>public class Program { static Semaphore semaphore; [STAThread] static void Main(string[] args) { StartupConfigurator startConfig = new StartupConfigurator(); semaphore = new Semaphore(1, 1, "2ndApp"); semaphore.WaitOne(); startConfig.StartProcessCommandLine(args); semaphore.Release(1); Environment.Exit(1); } } </code></pre> <p>This works fine as long as the applicatin exits gracefully and the semaphore is released. In the 2nd app, there is a line of code which while executing just hangs (it is basically trying to login to a server, but fails, reasons are out of the scope for the 2nd app to know), it just hangs there. Because of the way the service is written, it will start an new instance of 2nd app everytime a new file is created. It can be adding of a new file or adding multiple files at once to the watched folder. For example, if 4 files are dropped to the watched folder, the service will launch 4 instances of the 2nd app. The 2nd app finishes a file, the semaphore is released and the next file is processed. This works fine if it does that way,i.e., it exits gracefully. In the case where it hangs, the only way to resume processing is to close all the instances from the Task Manager, ending just the troubled instance will not help either, all of them have to be ended.</p> <p>There was discussion on the similar topic <a href="https://stackoverflow.com/questions/1119841/net-console-application-exit-event">here</a> but I am not able to know how to use it. Is there a way to for the 2nd app to know that it has been forcefully ended and the semaphore can be released.</p> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload