none
How does the async Task mechanism to catch the HandleProcessCorruptedStateException exception?

    Question

  • we implement the unmanaged code in the past, we usually decorate the Attribute HandleProcessCorruptedStateException and SecurityCritical.

    but I want to use Task.FromResult to wrap those unmanaged code, the vs intelligence warn me I can't use the ATTRIBUTE SecurityCritical..

    why? and how we catch the exception with single HandleProcessCorruptedStateException  Attribute;

    please help me ,thank you

    ------------------------------------Update-------------------------------------------------------------

    i know what you say, but It seems like you don't get my point.

    my point is when we call unmanaged code

    for example(non-task method):

    [DllImport("sm.dll",EntryPoint="method")]
    // this is the normal method
    public static extern void method();
    
    [HandleProcessCorruptedStateException]
    
    [SecurityCritical]
    
    public void methodwrapper()
    
    {
    
        try
    
       {
    
           RuntimeHelper.EnsureSufficientExecutionStack();
    
           Method();
    
       }
    
    ..........
    
    }


    but when I want to use async task.

    [DllImport("sm.dll",EntryPoint="method")]
    
    public static extern void method();
    
    [HandleProcessCorruptedStateException]
    
    [SecurityCritical]
    // here will throw exception on compiler time, 
    //this Security attribute"SecurityCritical" 
    //can not apply to async method
    
    public void methodwrapper()
    
    {
    
        try
    
       {
    
           RuntimeHelper.EnsureSufficientExecutionStack();
    
           Method();
    
       }
    
    ..........
    
    }

    here is my question, why ?

    note:I can change my async method to return Task only, and await at other method block. 


    Friday, April 21, 2017 8:40 AM

All replies

  • Exceptions that occur while executing tasks are captured and triggered at the point you call Result or Wait or await. So you would put this logic in the code that will be doing that, not the code running as a task. Note that running code in a task uses the thread pool by default so truly critical errors may still bring everything down. You'll need to test the code to ensure it is handling the errors as you expect.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, April 21, 2017 1:46 PM
    Moderator