c# - Is there any way I can call this getter and consistently get the expected result? -


i need use property getter 3rd-party api, , accessing getter sometimes hangs entire application, sometimes works (in dev/debugger). , if deploy production server, host application sometimes works expected, sometimes dies appcrash in kernel32.

i couldn't understand how simple getter freeze or crash anything, though maybe property method in disguise, , used favorite decompiler find out.

this scheme/pattern came across:

internal myclass() : isomeinterface {     locker = new object(); }  public object locker { get; private set; }  private someobject _myproperty; public someobject myproperty {         {         lock (locker)         {             if (_myproperty== null)                 myproperty = somecondition ? somemethod() : something.else;             return _myproperty;         }     }      private set     {         _myproperty = value;     } } 

am correct believe whenever _myproperty null, getter calls setter , there's race condition bug here may causing random/occasional freezing i'm experiencing? how can confirm/infirm freezing deadlock?

what correct implementation? i'm led believe if setter had lock (locker) (i don't locker being publicly accessible either, sounds asking trouble), , getter assign _myproperty instead of calling setter, it'd thread-safe.

lastly, there way can call getter without requiring 3rd party ship "fixed" version, , without freezing?

i have tried calling main ui thread, have tried calling task / background thread, have tried sleep(200) before call, have tried setting timeout on task... no matter do, don't seem able consistently call getter , reliably expected result (it does work... sometimes).

the locker object public, it's defined in internal class access via interface, of course doesn't expose it. there way can use reflection acquire lock outside (monitor.tryenter(magicallyobtainedlocker)) , work? or getter borked?

am correct believe whenever _myproperty null, getter calls setter , there's race condition bug here may causing random/occasional freezing i'm experiencing? how can confirm/infirm freezing deadlock?

no. in c#, lock reentrant same thread. can see in documentation monitor.enter:

it legal same thread invoke enter more once without blocking; however, equal number of exit calls must invoked before other threads waiting on object unblock.


don't locker being publicly accessible either, sounds asking trouble

i agree here. more culprit. fact locker public means else in library locking on object, could cause of deadlock.

if can run through visual studio concurrency profiler, should able pause @ deadlock, , see objects blocking @ point in time.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -