0

I have a small uwp app running with mvvm pattern that needs to load a big JSON file at some point during execution. I want to print a message box with an indeterminate progress ring control until the deserialization is complete. I thought of awaiting for the result of JsonConvert.DeserializeObject asynchronously and printing the message box in the meantime. Something like this :

MyClass Deserialize(string text, JsonSerializerSettings settings)
{
    MyClass result = JsonConvert.DeserializeObject<MyClass>(text, settings);
}

private async void LoadingScreenAsync()
{
    var dialog = new MessageDialogWithFancySpinningRing(); // I actually don't know how to do it though ;)
    await dialog.ShowAsync();
}

var deserializeTask = Deserialize(MyText, MySettings);
Var LoadingScreenTask = LoadingScreenAsync();

MyClass Result = await deserializeTask;
await dialog.ShowAsync();

// ... continue with result

Though I haven't tried it yet because, from what I've read, I figured it wouldn't work since Deserialize and JsonConvert.DeserializeObject aren't async methods. It seams I would need to run the deserialization on another thread for that which looks like a lot of trouble for such a simple thing.

Any ideas ?

Marcel Barc
  • 137
  • 1
  • 9
  • 1
    Just a thought. Instead of Message Box why dont you try waiting cursor until your Json get de-serialized. I never tried MessageBox with progress ring while running Json Convert. – Mdyahiya May 15 '19 at 21:31
  • That a good solution though I'd like to explain stuff in the message box and allow the user to pick settings for what the application will do next (a bit like in some installation programs) – Marcel Barc May 15 '19 at 21:35
  • Found something interesting. Please check https://stackoverflow.com/questions/1952201/display-progress-bar-while-doing-some-work-in-c – Mdyahiya May 15 '19 at 22:07
  • Thanks i appreciate your research though Backgroundworker is something like the ancestor of async/await and is more of a winform thing from what I've understood. – Marcel Barc May 15 '19 at 22:15
  • This is not your real code, and would not compile, whats all this stuff `Var LoadingScreenTask = LoadingScreenAsync();` an async void will not return a task – TheGeneral May 15 '19 at 23:33
  • @TheGeneral, this is not my real code, just a quick test I made out of context, and the fact it doesn't compile is one of the reasons I ask the question ;) Should I just write await `LoadingScreenAsync();` instead ? – Marcel Barc May 16 '19 at 12:37

1 Answers1

1

How to show a message box with indeterminate progress ring while deserializing a big JSON file?

At first, you need to invoke Deserialize method in non-UI thread, then you need to invoke dialog Hide after Deserialize finished.

Deserialize is a time consuming operation. please run it in the Task and add the await key word in head of method . For more you could refer the following code.

private ContentDialog noWifiDialog;
private async void DisplayNoWifiDialog()
{
    noWifiDialog = new ContentDialog()
    {
        Title = "Deserialize Json ",
        Content = new ProgressRing() { Name = "MyProsress", IsActive = true },
        CloseButtonText = "Ok"
    };

    await noWifiDialog.ShowAsync();
}

private Task Deserialize()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 1000000000; i++)
        {
         // Simulated time-consuming operation
        }
    });
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
    DisplayNoWifiDialog();
    await Deserialize();
    noWifiDialog.Hide();
}
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36