Can this unexpected behavior of PrepareConstrainedRegions and Thread.Abort be explained?

前端 未结 3 1138
庸人自扰
庸人自扰 2021-01-06 01:52

I was playing around with Constrained Execution Regions tonight to better round out my understanding of the finer details. I have used them on occasion before, but in those

3条回答
  •  心在旅途
    2021-01-06 01:58

    Your unexpected behavior is due to the fact that your code has the maximum reliability.

    Define the following methods:

    private static bool SwitchToggle(bool toggle) => !toggle;
    
    [ReliabilityContract(Consistency.WillNotCorruptState,Cer.Success)]
    private static bool SafeSwitchToggle(bool toggle) => !toggle;
    

    And use them instead of the body of your while cycle. You will notice that when calling SwitchToggle the cycle becomes abortable and when calling SafeSwitchToggle it is no more abortable.

    The same goes if you add whichever other methods inside the try block that is not having a Consistency.WillNotCorruptState or Consistency.MayCorruptInstance.

提交回复
热议问题