简体   繁体   English

捕获在不同线程中抛出的异常

[英]catch exception that is thrown in different thread

One of my method ( Method1 ) spawns a new thread.我的方法之一( Method1 )产生一个新线程。 That thread execute a method ( Method2 ) and during exectution an exception is thrown.该线程执行一个方法( Method2 )并在执行期间抛出异常。 I need to get that exception information on the calling method ( Method1 )我需要获取有关调用方法( Method1 )的异常信息

Is there someway I can catch this exception in Method1 that is thrown in Method2 ?有什么方法我能赶上在这个异常Method1是在抛出Method2

One of my method ( Method1 ) spawns a new thread.我的一个方法( Method1 )产生了一个新线程。 That thread execute a method ( Method2 ) and during exectution an exception is thrown.该线程执行一个方法( Method2 ),并且在执行过程中引发异常。 I need to get that exception information on the calling method ( Method1 )我需要获取有关调用方法( Method1 )的异常信息

Is there someway I can catch this exception in Method1 that is thrown in Method2 ?有什么方法我能赶上在这个异常Method1是在抛出Method2

One of my method ( Method1 ) spawns a new thread.我的一个方法( Method1 )产生了一个新线程。 That thread execute a method ( Method2 ) and during exectution an exception is thrown.该线程执行一个方法( Method2 ),并且在执行过程中引发异常。 I need to get that exception information on the calling method ( Method1 )我需要获取有关调用方法( Method1 )的异常信息

Is there someway I can catch this exception in Method1 that is thrown in Method2 ?有什么方法我能赶上在这个异常Method1是在抛出Method2

One of my method ( Method1 ) spawns a new thread.我的一个方法( Method1 )产生了一个新线程。 That thread execute a method ( Method2 ) and during exectution an exception is thrown.该线程执行一个方法( Method2 ),并且在执行过程中引发异常。 I need to get that exception information on the calling method ( Method1 )我需要获取有关调用方法( Method1 )的异常信息

Is there someway I can catch this exception in Method1 that is thrown in Method2 ?有什么方法我能赶上在这个异常Method1是在抛出Method2

One of my method ( Method1 ) spawns a new thread.我的一个方法( Method1 )产生了一个新线程。 That thread execute a method ( Method2 ) and during exectution an exception is thrown.该线程执行一个方法( Method2 ),并且在执行过程中引发异常。 I need to get that exception information on the calling method ( Method1 )我需要获取有关调用方法( Method1 )的异常信息

Is there someway I can catch this exception in Method1 that is thrown in Method2 ?有什么方法我能赶上在这个异常Method1是在抛出Method2

Here is the code that I used to throw the exception back to the main thread to be caught.这是我用来将异常抛出回主线程以进行捕获的代码。

class Program
{
    static void Main(string[] args)
    {
        CancellationTokenSource cancelToken = new CancellationTokenSource();
        Exception taskException = null;

        var timerTask = Task.Factory.StartNew(() =>
        {
            for (;;)
            {
                if (cancelToken.IsCancellationRequested)
                    break;

                ContinuousTask();

                Thread.Sleep(400);

            }
        }, cancelToken.Token).ContinueWith((t, o) => {
            taskException = t.Exception;
            ((Thread)o).Interrupt();
        }, Thread.CurrentThread, TaskContinuationOptions.OnlyOnFaulted);

        try
        {
            
            //do a bunch of tasks here

            //want to skip the do while and go to the catch if exception is thrown
            do
            {
                System.Threading.Thread.Sleep(200);
            } while (true);

        }
        catch
        {
            if (taskException != null)
                Console.WriteLine(taskException.Message);
        }
        
    }

    private static int _loopCounter = 0;
    public static void ContinuousTask()
    {
        int counter = 0;

        do
        {

            if (_loopCounter >= 3)
                throw new Exception("error");

            if (counter >= 5)
                break;

            counter += 1;
            System.Threading.Thread.Sleep(100);

        } while (true);

        _loopCounter += 1;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM