One of my method ( Method1
) spawns a new thread. That thread execute a method ( Method2
) and during exectution an exception is thrown. I need to get that exception information on the calling method ( Method1
)
Is there someway I can catch this exception in Method1
that is thrown in Method2
?
One of my method ( Method1
) spawns a new thread. That thread execute a method ( Method2
) and during exectution an exception is thrown. I need to get that exception information on the calling method ( Method1
)
Is there someway I can catch this exception in Method1
that is thrown in Method2
?
One of my method ( Method1
) spawns a new thread. That thread execute a method ( Method2
) and during exectution an exception is thrown. I need to get that exception information on the calling method ( Method1
)
Is there someway I can catch this exception in Method1
that is thrown in Method2
?
One of my method ( Method1
) spawns a new thread. That thread execute a method ( Method2
) and during exectution an exception is thrown. I need to get that exception information on the calling method ( Method1
)
Is there someway I can catch this exception in Method1
that is thrown in Method2
?
One of my method ( Method1
) spawns a new thread. That thread execute a method ( Method2
) and during exectution an exception is thrown. I need to get that exception information on the calling method ( Method1
)
Is there someway I can catch this exception in Method1
that is thrown in 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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.