C # need to raise an event when in the LOCK area

I have a filehandler class that moves xdoument to cache using locks. Psuedo Code:

public FetchDocument() {
    var xdoc = Cache[_Key1];
    if (xdoc == null) {
        lock(_lockobject) {
            // in case prev.thread has done upload will currth read waiting
            xdoc = Cache[_Key1];

            if (xdoc == null) {
               .... Code to grab from file and add to catch - works great.....
            }
        }
    }
    xdoc = Cache[_Key1];

}

Now I want to open an event that is fired after the file has been loaded, but before it was cached and which has an interrupt flag in the event.

The problem is, how can I only fire the event only for the current (unlocked) thread?

another thread may be blocked, expecting that thread to clear the lock code

thank

Martin

+3
source share
2 answers

, . , Func FetchDocument, :

public XDocument FetchDocument(FUnc<XDocument, bool> cacheNewDocument) {
    var xdoc;        
    lock(_lockobject) {
        // in case prev.thread has done upload will currth read waiting
        xdoc = Cache[_Key1];

        if (xdoc == null) {
           xdoc = .... Code to grab from file and add to catch - works great.....

           if (cacheNewDocument(xdoc)){
               Cache.Add(_Key1, xdoc)
           }
           else{
               return xdoc;
           }
        }
    }

    return xdoc;
}

, Func , true\false, abort.

, , " () .

, , Cache . , , , . .

0

, , - .
Monitor.TryEnter .

, .

0

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


All Articles