c# - using constrained execute regions -


I have a Visual Studio 2008 C # .NET 3.5 application that P / file as a parameter to a native way Accepts handle. Basically, I was just using the filestream. SafeFileHandle.DangerousGetHandle () but to get a handle file, after turning the FX COP, I was warned about it so, after a little research I discovered "resistive executed areas" is new to me and I about it I have not seen a lot of information in this. I was hoping that another experienced person could see and verify that I did it correctly.

  class MyClass {public static bool Write (string filename) using {(VAR FS = new SystemkIOkFileStream (filename, System.IO.FileMode.Create, System. IO.FileAccess. Write, System.io.fileshare.non)) {Got it; Bull results; System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions (); {} Finally try {fs.SafeFileHandle.DangerousAddRef (RF Got_Hold); Results = Basic methods. Foo (fs.SafeFileHandle.DangerousGetHandle ()); If (get_hand) fs.SafeFileHandle.DangerousRelease (); } Return results; }}} Internal sealed class NativeMethods {[DllImport ( "mylib.dll", EntryPoint = "Fu", CallingConvention = CallingConvention.StdCall, charset = CharSet.Unicode, ExactSpelling = true, SetLastError = True)] Fu bool public static exile (Interrupt HFile); }   

Thanks, Paul HD

You do many things here Are there.

  1. ThreadAbortExceptions execute the code at the block end to prevent your security code being executed.

  2. Before try / finally move you think that enough thread check stack space is PrepareConstrainedRegions which basically except find some phones that does not at least have Some method calls can then be made so that your safe code is not caught off guard by a StackOverFlowException exists to be present.

    So, you look as safe as you can to be safe. About CER officially, it has been said that CLR recognizes this endeavor / eventually blocks and extra measures are also made. There is not much difference between what I have seen, apart from that, after the exception of your CER code, the outoffime exceptions will also be delayed.

    In order to make sure that your code meets your expectations, you should test for these things.

    • pile after
    • memory
    • Thread.Abort

      reliable writing code really hard and Even most BCL classes are not against things which Joe Duffy describes. Even if your code BCL code may not fail. The benefits will not be linked until you have a major part of the very BCL code can tackle these extreme situations in a well defined manner.

      Yours, Aloise Cross

Comments