Disconnect blocking from a different thread than the owner of the monitor

I have a critical section in the code that is limited to two function calls, for example Start()and End(). They use Monitorto block other threads at runtime. Now my problem is that if any thread does not call End()for any reason, my whole process is in trouble, as each thread expects this to Monitorbe released.

Of course, I could use TryEntera timeout so that I won’t wait forever, but this will not release the blocked one Monitor, so my program will receive this timeout every time from now on.

Is there a way to release the lock Monitorfrom another thread if the specified timeout is completed?

void Start(){ Monitor.Enter(obj); }

void End(){ Monitor.Exit(obj); }

EDIT: We call Excel through com interop, and we cannot be sure that the Excel process will work as expected. Please note that this is a web application, so the inability to deal with this case is fatal. Start()called the first time, the request calls the excel function, End()called at the end of the request. There is always a chance that the excel process will start to freeze.

EDIT2: Now I want the owner of the owner of the lock in the variable and at the dead end I could kill this thread. Wouldn't that free the castle?

                        if (Monitor.TryEnter(excelLocker, 10000) == false)
                        {
                            excelOwner.Abort();
                            excelOwner = null;
                        }
                        else
                        {
                            excelOwner = Thread.CurrentThread;
                        }
+3
source share
3 answers

, , - , . , "" - . , , , .

, lock, Enter Exit, Monitor.

, , start request end. lock, Excel, , Enter Exit.

FYI a lock Monitor .

    lock(_syncObj)
    {
        //Do stuff
    }

    //Is equivalent to

    Monitor.Enter(_syncObj);
    try
    {
        //Do stuff
    }
    finally
    {
        Monitor.Exit(_syncObj);
    } 

lock, Excel :

    //Client code
    ExcelUtil.DoStuff("bling")

    //...

    //Util class manages call to Excel and locking.
    public static class ExcelUtil
    {
        private static readonly object SyncObj = new object();

        public static void DoStuff(string someParam)
        {
            //Guaranteed locking and unlocking even if an exception occurs
            lock (SyncObj)
            {
                DoSomeStuffWithExcelFuncA();
                DoSomeStuffWithExcelFuncB();
            }
        }

        private static void DoSomeStuffWithExcelFuncA()
        {
            //...
        }

        private static void DoSomeStuffWithExcelFuncB()
        {
            //...
        }
    }

, Excel? , Excel ASP.Net. , , , . Excel , . , Excel. , Excel ?

, , ( Excel) - .

+2

, .

, , . ++, , , ( , - ).

0

→ , - End() - , , , .

, , - End():

  • , , , . , ( , ), , - , Thread.Abort().

  • . try/finally.

Excel , , , . Code Review. StackExchange .

, , , Monitor.TryEnter(object, ref bool). , , , .

0

Source: https://habr.com/ru/post/1789810/


All Articles