C# 在wpf应用程序中混合并行和异步

C# 在wpf应用程序中混合并行和异步,c#,wpf,asynchronous,mvvm,mvvm-light,C#,Wpf,Asynchronous,Mvvm,Mvvm Light,我需要在WPF启动代码中混合并行和异步概念: protected override async void OnStartup(StartupEventArgs e) { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += currentDomain_UnhandledException; ViewModelLocator locator =

我需要在WPF启动代码中混合并行和异步概念:

 protected override async void OnStartup(StartupEventArgs e) 
 {
     AppDomain currentDomain = AppDomain.CurrentDomain;
     currentDomain.UnhandledException += currentDomain_UnhandledException;
     ViewModelLocator locator = (ViewModelLocator)App.Current.Resources["Locator"];
     SimpleIoc.Default.GetInstance<MainViewModel>();
     SimpleIoc.Default.GetInstance<IndexViewModel>();
     Func<Task> fsignHandler = async () => { await Task.Run(() => { SimpleIoc.Default.GetInstance<Fiche_SignaletiqueViewModel>(); Debug.WriteLine("fsign loaded"); }); };
     Func<Task> formationHandler = async () => { await  Task.Run(() => { SimpleIoc.Default.GetInstance<FormationsViewModel>(); }); };
     await Task.WhenAll(fsignHandler(), formationHandler());
}
启动时受保护的覆盖异步无效(StartupEventArgs e)
{
AppDomain currentDomain=AppDomain.currentDomain;
currentDomain.UnhandledException+=currentDomain\u UnhandledException;
ViewModelLocator定位器=(ViewModelLocator)App.Current.Resources[“定位器”];
SimpleIoc.Default.GetInstance();
SimpleIoc.Default.GetInstance();
Func fsignHandler=async()=>{await Task.Run(()=>{SimpleIoc.Default.GetInstance();Debug.WriteLine(“fsign-loaded”);};
Func formationHandler=async()=>{await Task.Run(()=>{SimpleIoc.Default.GetInstance();});};
wait Task.WhenAll(fsignHandler(),formationHandler());
}
我发现这些问题:

  • 在输出窗口中,我没有收到加载的消息
    fsign
  • 我使用带有IoC的MVVM light
    SimpleIOC
    :当我导航到菜单
    Fiche\u Signaletique
    (使用
    Fiche\u SignaletiqueViewModel
    作为视图模型)时,类Fiche\u SignaletiqueViewModel将被实例化,这表明从未执行过
    fsignHandler
    我需要知道:

    • 这些问题的原因是什么
    • 我犯了什么错误
    • 如何修复我的代码

    您是
    新的
    正在整理您的
    任务
    实例,但从未启动它们(因此,它们从未完成)。使用
    任务。改为运行
    。@KirillShlenskiy正常,但任务的执行变为同步@LamloumiAfif:它不应该…@JonSkeet plz请看我的编辑,使用此代码,接口将被阻止,直到所有任务的执行完成为止。它不应该被阻止(如果您正在同步执行的对
    GetInstance
    的两个调用快速执行)。如果你能把一个简短但完整的程序放在一起演示这个问题,那么在这一点上它将非常有帮助。理想情况下,不要使用SimpleIOC和MVVM灯光—尽可能简化它。