Abandoned named semaphore not released

前端 未结 2 1125
你的背包
你的背包 2020-12-30 16:02

when a C# program holds a named semaphore, it does not seem to be released when the application is terminated early (for example by pressing Ctrl+C or closing the console wi

2条回答
  •  攒了一身酷
    2020-12-30 16:37

    You can write the mySemaphore.Release(); in class destructor

    class Program
    {
        ~Program()  // destructor
        {
            mySemaphore.Release();
        }
    }
    

    or add a finally pharse to your try\catch

    try{}
    catch{}
    finally 
    {
        mySemaphore.Release();
    }
    

    If you are using asp.net you can also use Application_End which located in Global.asax.cs

    protected void Application_End(Object sender, EventArgs eventArgs)
    {
        mySemaphore.Release();
    }
    

提交回复
热议问题