1

メソッドの実行直前に読み込みインジケータを開始したい。メソッドの実行にはエンティティ フレームワークの作業が含まれるため、そのタイプのコードを新しいスレッド bc エンティティ フレームワークに入れることはできません (できません)。エンティティ フレームワークはスレッド セーフではありません。したがって、基本的に以下のメソッドでは、最初の行を実行して UI を更新し、戻って残りのコードを実行します。何か案は?

 public async void LoadWizard()
 {
    IsLoading = true; //Need the UI to update immediately 

    //Now lets run the rest (This may take a couple seconds)
    StartWizard();
    Refresh(); 
 }

私はこれを行うことはできません:

 public async void LoadWizard()
 {
    IsLoading = true; //Need the UI to update immediately 

    await Task.Factory.StartNew(() =>
    {
        //Now lets run the rest (This may take a couple seconds)
        StartWizard();
        Refresh(); //Load from entityframework
    });

    //This isn't good to do entityframework in another thread. It breaks.

 }
4

2 に答える 2

0

ビジー インジケーターの可視性が IsLoading プロパティにバインドされていると仮定すると、StartWizard または Refresh メソッドで「何か」が間違っています。StartWizard メソッドと Refresh メソッドは、データ ソースからのみデータを読み込む必要があります。ローディング メソッドで UI の状態を変更するコードを含めてはなりません。ここにいくつかの擬似コードがあります..

public async void LoadWizard()
 {
    IsLoading = true; 

    StartWizard();
    var efData = Refresh(); 

    IsLoading = false;

    //update values of properties bound to the view
    PropertyBoundToView1 = efData.Prop1;
    PropertyBoundToView2 = efData.Prop2;
 }

public void StartWizard()
{
  //do something with data that are not bound to the view
}

public MyData Refresh()
{
   return context.Set<MyData>().FirstOrDefault();
}
于 2014-05-24T17:40:17.137 に答える
0

優先度を Render に設定して、UI ディスパッチャーで空のデリゲートを呼び出すことができます。これにより、UI は、キューに入れられたすべての操作を Render と同等以上の優先度で処理します。(Render ディスパッチャーの優先度で UI が再描画されます)

public async void LoadWizard()
{
   IsLoading = true; //Need the UI to update immediately 

   App.Current.Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);

   //Now lets run the rest (This may take a couple seconds)
   StartWizard();
   Refresh(); 
}
于 2014-05-23T17:22:52.147 に答える