Catch exceptions at parent thread throw by call back methodRSS

1 reply

Last post May 05, 2017 09:35 AM by Yuk Ding

  • Catch exceptions at parent thread throw by call back method

    May 05, 2017 03:25 AM|darthvivek|LINK

    Hello All

    I have a WCF web Method that takes too long to execute. So during the processing i create a call back method that counts down to 30 seconds. So once this countdown elapses, i need this method to throw an exception to the parent thread [web method thread] so that i can terminate this WS request.

    public class Example
            public delegate void AsyncMethodCaller();
            public static void WorkerMethod()
                Console.WriteLine("1: Worker Process called. {0}", DateTime.Now);
                Console.WriteLine("2. Exiting from WorkerMethod.{0}", DateTime.Now);

            public static void SomeOtherMethod(IAsyncResult result)
                AsyncMethodCaller dlgt = (AsyncMethodCaller)result.AsyncState;

                // Call EndInvoke to retrive the results.
                throw new TimeoutException("exception in ProcessList");

            static void Main(string[] args)
                    AsyncMethodCaller asyncCaller = new AsyncMethodCaller(WorkerMethod);
                    AsyncCallback callBack = new AsyncCallback(SomeOtherMethod);
                    IAsyncResult result = asyncCaller.BeginInvoke(callBack, asyncCaller);
                    //Console.WriteLine("4. Worker method has been called.");
                    Console.WriteLine("0.Main thread Sleeps.{0}", DateTime.Now);
                    Console.WriteLine("5.Main thread Wakes.{0}", DateTime.Now);
                    Console.WriteLine("6.Main thread compleates.{0}", DateTime.Now);
                catch (TimeoutException ex)
                    Console.WriteLine("7. Catching TO exception.{0}", DateTime.Now);
                catch (Exception ex)
                    Console.WriteLine("8. Catching exception.{0}", DateTime.Now);

    The issue i'm facing here is at "SomeOtherMethod' method, the exception is thrown, but this does not return to the main method.

    I want this exception to be caught at the main method

  • Re: Catch exceptions at parent thread throw by call back method

    May 05, 2017 09:35 AM|Yuk Ding|LINK

    Hi darthvivek,

    If you got the long time to excute, the failed request tracing could help you trace the issue the performance:,

    If you need any code suggestion, you could post the error message to or msdn forum:

    Best Regards,

    Yuk Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.