For the sake of simplicity I reproduced my Xamarin nUnit testing error as a console aplication and it shows the same problem that I cannot understand. So first the code that works and second the code that doesn't work.
Simple console app
public class Working
{
private MyViewModel _viewModel;
public Working()
{
Console.WriteLine("Start");
_viewModel = new MyViewModel();
}
static void Main(string[] args)
{
Working prog = new Working();
prog.Print();
}
public void Print()
{
_viewModel.NewSurveyCommand.Execute(null);
}
}
public class MyViewModel
{
public MyViewModel()
{
NewSurveyCommand = new MyCommand(RunTest);
}
public ICommand NewSurveyCommand { get; private set; }
private void RunTest()
{
Console.WriteLine("Running...");
Thread.Sleep(1000);
Console.WriteLine("Test done");
}
}
public class MyCommand : ICommand
{
private Action _action;
public MyCommand(Action action)
{
_action = action;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_action.Invoke();
}
}
This works fine, the console prints running... then prints test done in one second. Now the second async version which only prints running...
public class Program
{
private ViewModel _viewModel;
public Program()
{
Console.WriteLine("Start");
_viewModel = new ViewModel();
}
static void Main(string[] args)
{
Program prog = new Program();
prog.Go();
}
async void Go()
{
await Print();
}
public async Task Print()
{
await Task.Run( () => _viewModel.NewSurveyCommand.Execute(null) );
}
}
public class ViewModel
{
public ViewModel()
{
NewSurveyCommand = new Command(async () => await RunTest());
}
public ICommand NewSurveyCommand { get; private set; }
public async Task RunTest()
{
Console.WriteLine("Running...");
await Task.Run( () => Thread.Sleep(1000));
Console.WriteLine("Test done");
}
}
public class Command : ICommand
{
private Action _action;
public Command(Action action)
{
_action = action;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_action.Invoke();
}
}
}
So the second case executes only part of the code, when it gets to await Task.Run( () => Thread.Sleep(1000)); it just leaves the method to never come back. I don't understand why and how to solve that. Has anyone ever come across the same problem. Thanks.