修复 "A method was called at an unexpected time" 错误的最佳方法是什么

What is the best way to fix "A method was called at an unexpected time" error

首先,我已经看到 this question,并且我(有点)理解为什么会出现此异常,我想知道修复它的最佳方法是什么。我的代码看起来有点像这样(这是一个 WinRT 应用程序):

//Here is my App constructor:
public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;

    //Initializing the model
    _model = new Model();
    _model.LoadData();
}

//the LoadData method looks like this:
public async void LoadData()
{
    StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    StorageFile file = await folder.GetFileAsync(@"Assets\Data.json");
    string data = await FileIO.ReadTextAsync(file);

    var dataList = JsonConvert.DeserializeObject<List<MyDataClass>>(data);
    // From time to time (pretty rarely, honestly) this line causes the
    // "A method was called at an unexpected time" thing:
    var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
    foreach (var item in dataList)
    {
        //do some stuff
        //<...>
        await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () => {
            //do some stuff in the UI thread
        });                
    }
}

显然,LoadData 方法 async void 并不是最好的解决方案。但是,如您所见,我必须在其中执行一些异步函数调用(以从文件中读取数据)。现在我能想到两种可能的解决方案:

  1. LoadData 更改为 public async Task LoadData() 并将其在应用程序构造函数中的调用更改为 _model.LoadData().GetAwaiter().GetResult(); 以便 运行 它同步;
  2. LoadData更改为public void LoadData(),并将其中的所有await调用更改为使用等待程序,例如StorageFile file = folder.GetFileAsync(@"Assets\Data.json").GetAwaiter().GetResult().

哪一个是更好的解决方案,或者更好的是,有没有其他正确的方法可以在应用程序启动时 运行 异步代码?另外,为什么 "A method was called at an unexpected time" 错误发生在调度程序行上?

Which of these is a better solution

两个预期的解决方案 block - 具体来说,它们 block on I/O。让我们考虑一下阻塞,但这次不是从应用程序的角度 "I need this data before I can display what I want" 考虑,而是从运行时的角度考虑。

运行时绝对肯定不应该阻塞UI。阻止 UI 会阻止用户,这简直是一种不可接受的用户体验。这就是为什么整个移动世界都是异步优先的;这让许多桌面开发人员感到震惊。桌面应用程序 应该 是异步优先的,但移动应用程序 必须 是异步优先的。

因此,从运行时的角度来看,当它启动一个应用程序时,该应用程序必须显示一些东西。立即地。现在。 I/O 不是一个选项。它不一定是完美的应用程序主屏幕,但应用程序必须同步显示某些内容

这就是阻止不起作用的原因。自动应用商店代码分析应该拒绝这两种阻止方法。

is there any other proper way to run async code on the application startup?

是的,但应用程序必须妥协。在向用户显示其漂亮的、完整的第一个屏幕之前,它不能在执行 I/O 时阻塞 UI。一些桌面应用程序可以逃脱这一点(即使它们也不应该),但这不是移动应用程序的选择。

相反,应用程序应(同步)加载并显示 "loading..." 状态。这可以是初始屏幕,也可以是实际的主屏幕,只是用 "loading..." 消息或微调器代替数据。这满足了运行时的要求。当然,在返回之前,应用程序还应该启动异步操作来检索数据,稍后update UI显示它真正想要显示的内容(包含数据的常规视图)。

这可以像使用异步事件一样简单(正如@marcinax 评论的那样):

protected override async void OnLaunched(...)
{
  ... // Initialize UI to "Loading" state.

  _model = new Model();
  await _model.LoadData();

  ... // Update UI with data in _model.
}

就个人而言,我更喜欢将所有 UI 代码保留在 UI 层(不是 Model 的一部分),因此如果您有任何显式(非数据绑定) ) UI初始化数据,应该在// Update UI.

之后

但是,如果您发现自己在应用程序中多次执行此操作(例如,每次 window/page 都需要加载数据),那么您可能更喜欢像我的 MSDN 中描述的 asynchronous data-binding solution文章.

Also, why is the "A method was called at an unexpected time" error happening on the dispatcher line?

我不太确定,但我怀疑调度员还没有 运行。

将 UI 代码保留在 model/VM 层之外的一个非常好的副作用是不再需要显式 CoreDispatcher 代码。总有更好的解决办法。