I'm tryig to implement a Save/Open feature in my program, and I'm trying to do it serializing what I have to save in order to be able to easily load it when the user needs using deserializing. Newtonsoft is the library of choice.
Problem is that the file can end up being very large, so deserializing asynchronously is a must.
What I serialize and deserialize is an object of class Validation
this is what I'm trying to do:
public async static Task<Validation> CreateFromSaveFile(Validator Outer, string FileName)
{
StreamReader SR = new StreamReader(FileName);
JsonTextReader JR = new JsonTextReader(SR);
var Serializer = new JsonSerializer();
Console.WriteLine("1");
var task = Task.Factory.StartNew(() => Serializer.Deserialize<Validation>(JR));
Validation Res = await task;
Console.WriteLine("2");
JR.Close();
SR.Close();
return Res;
}
Now: 1 gets printed, 2 doesn't. It gets stuck awaiting the task. I know it should work because running the same code synchronously does what it's supposed to and works just fine. Obviously there's something I'm not getting about how async/await programming works and how it should be used.
Anybody any idea?
EDIT: I've been asked to post the code that calls this code. Here we go:
public async Task<Validation> GetStartingValidation(Menu Outer)
{
Validation Res = await Validation.CreateFromSaveFile(this, @"Some\Path.txt").Result;
Console.WriteLine("Done");
return Res;
}