Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7538149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:02:42+00:00 2026-05-30T07:02:42+00:00

I have an windows service that monitors a folder for new files being created.when

  • 0

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:

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);
    }
}

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.

There was discussion on the similar topic here 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.

Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:02:44+00:00Added an answer on May 30, 2026 at 7:02 am

    Is there a way to for the 2nd app to know that it has been forcefully ended and the semaphore can be released.

    No, when a process is killed, there is no chance to cleanup, execute finally blocks, etc. See this. Your best bet is to launch the hang-y operation on a separate thread and call join on the thread to wait for the operation to complete. A sample:

    bool completed = false;
    try
    {
       Thread thread = new Thread(() => startConfig.StartProcessCommmandLine(args));
       thread.IsBackground = true;
       thread.Start();
       bool completed = thread.Join(60000); // 60s timeout
    }
    finally
    {
       semaphore.Release(1);
    }
    Environment.Exit(completed ? 0 : 1);
    

    Typically, an exit code of 0 is considered normal. The try/catch is important because if your thread throws an unhandled exception we want to make sure to release the semaphore before the process is brought down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already created a windows service that monitors the network for some specific
I have written a service that monitors a file drop location for files from
I have created a Windows Service that will be calling out to some COM
I have a program that is stored in program files folder. I created windows
I have a Windows service that monitors several mailboxes for new mail. Once new
I have a Windows service written in C# which monitors a folder for files
I have a Windows Service that takes the name of a bunch of files
A new client has recently asked me to develop a Windows service that will
I have to write a Windows Service application (no GUI) that will monitor an
I have a program that runs as a Windows Service which is processing files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.