I am using this way to execute async void methods with access to local variables:
static void RunAsync(object var1, object var2)
{
ThreadStart work = delegate
{
try
{
Statement1(var1);
Statement2(var2);
// etc
}
catch (Exception e) { }
};
new Thread(work).Start();
}
Debugging through unit test in Visual Studio often causes ThreadAbortException, but running tests (and running code) works fine.
What is causing this?
It happens because when you call RunAsync method it creates the thread but returns to TestMethod where the test finishes before the thread finishes. When the test finishes it tries to abort all the threads so you get that exception. You must wait until the thread finishes by using a Join.
Suggestion: