-1

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;
        }
  • side note (probably not related to your problem) : you should use `using` statements when manipulating your streams, instead of closing them manually. If there is an exception, they might not be properly closed / disposed – Pac0 Mar 21 '18 at 17:25
  • 2
    What is the code that calls this code? – Crowcoder Mar 21 '18 at 17:25
  • 2
    Please use `Task.Run` instead of `Task.Factory.StartNew` unless you are forced to .NET 4.0. Also, you could just await the task in the same sentence – Camilo Terevinto Mar 21 '18 at 17:29
  • Tried already with Task.Run, and it's not a solution to the problem. – Manuel Venè Mar 21 '18 at 17:30
  • 1
    That's not true asyhchronous code as your just spinning up another thread that gets blocked on the IO. It's better to find the async equivalent methods to do what you want instead (if possible). – juharr Mar 21 '18 at 17:31
  • @CamiloTerevinto : do you think problem reported is because of `Task.Factory.StartNew` ? – rahulaga-msft Mar 21 '18 at 17:31
  • 1
    do you block anywhere with .Result or .Wait()? – Crowcoder Mar 21 '18 at 17:31
  • 1
    @RahulAgarwal No, not at all, but it's not a good practice. The problem is most likely on the caller side, but that code wasn't posted – Camilo Terevinto Mar 21 '18 at 17:31
  • 2
    If it is a large file, shouldn't you be using this method? http://james.newtonking.com/archive/2017/03/21/json-net-10-0-release-1-async-performance-documentation-and-more – Rosdi Kasim Mar 21 '18 at 17:33
  • This will not deadlock as long as you are not using `Result` or `Wait()`. Please show us the full path of asynchronous calls. Where is `GetStartingValidation` being awaited? – FCin Mar 21 '18 at 18:05
  • Are you sure that is really your code? You can't await something you called .Result on. – Crowcoder Mar 21 '18 at 18:23

1 Answers1

0

Methods declared with the async keyword run synchronously until await is encountered. Once that happens control is returned to the caller of the async method. Once the call that the await keywords precedes is done, control is returned to the async method.

In your case this means that "2" will not be printed until deserialization is complete.

Matthias Grün
  • 1,466
  • 1
  • 7
  • 12
  • Well, that's exactly what I mean. The task never ends. Doing it synchronously works, 2 gets printed and result Res is returned. I just expected the same behaviour by this async code, but it's not the case – Manuel Venè Mar 21 '18 at 17:34
  • Oh sorry, I thought it did appear after deserialization is done. – Matthias Grün Mar 21 '18 at 17:37
  • @ManuelVenè You are most likely (99% sure) experiencing a deadlock, caused by the code that *calls* the code you posted. Please post that code and the type of project you are running – Camilo Terevinto Mar 21 '18 at 17:37