Delegate - Exceptions do not wait for EndInvoke () to be called

I have this code:

using System;
using System.Runtime.Remoting.Messaging;

class Program {
    static void Main(string[] args) {
        new Program().Run();
        Console.ReadLine();
    }

    void Run() {
        Action example = new Action(threaded);
        IAsyncResult ia = example.BeginInvoke(new AsyncCallback(completed), null);
        // Option #1:
        /*
        ia.AsyncWaitHandle.WaitOne();
        try {
          example.EndInvoke(ia);
        }
        catch (Exception ex) {
          Console.WriteLine(ex.Message);
        }
        */
    }

    void threaded() {
        throw new ApplicationException("Kaboom");
    }

    void completed(IAsyncResult ar) {
        // Option #2:
        Action example = (ar as AsyncResult).AsyncDelegate as Action;
        try {
            example.EndInvoke(ar);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

Many articles state that when I call BeginInvoke, everyone Exception(here from the method threaded) waits until I call EndInvokeand they are thrown out there. But it does not work, Exception("Kaboom") is "raw", and the program crashes. Can you help me?

Thank!

+5
source share
1 answer

. " ", , IDE . - "Kaboom" , . IDE ctrl + f5 f5.

, , IDE "":

enter image description here

; IDE . .

+4

All Articles