2016-10-05 6 views
-2

WPFウィザードコントロールにオーバーレイをロードする必要があります。 wpf extendedtitlitからbusyIndi​​catorツールを使用しています。非同期待機を使用するWPF WizardControlのカスタムビジーオーバーレイ

async awaitのコードは動作しますが、GUIスレッドはロックされます。待ち受けているときにメッセージを待ちます。

private async void Button1_Click(object sender, RoutedEventArgs e) 
     { 
     BusyIndicator.IsBusy = true; 
     BusyIndicator.IsEnabled = true; 
     BusyIndicator.BusyContent = "Please wait while Site is provisioned"; 

          await Task.Run(() => 
          { 
           LongRunningFunction(); 
          }); 

     BusyIndicator.IsBusy=false; 
     } 

BusyIndi​​catorのXAMLは次のとおりです。

<xctk:BusyIndicator x:Name="BusyIndicator" IsBusy="False" BusyContent="Please Wait"> 

</xctk:BusyIndicator> 

LonRunningFunctionはUIを更新しないWebサービスの呼び出しのみブール値を返している

public static bool LongRunningFunction(string URL) 
     { 

      bool IsPresent = CallWebservice() 
      return IsPresent; 
     } 

BusyIndi​​cator代わりに非同期呼び出しの前に発射していないよう )発行LongRunningタスクが完了したときに火災のように見える

2)asyncとawaitが使用されているときにguiオーバーレイを呼び出す正しいプロセスは何ですか?

+0

あなたはawaitの代わりに 'ThreadPool'を使ってみましたか?問題は、そのlongRunningTask内のUI要素を操作する場合、ディスパッチャーを使用する必要があることです。[Here](http://stackoverflow.com/a/37483878/2029607)は、コードでどのように使用できるかを示しています。 – XAMlMAX

+0

@XAMlMAX He *は*スレッドプールを使用しています。 'Task.Run'は、スレッドプールスレッドの作業をスケジュールします。 – Servy

+0

'LongRunningFunction'を見ることなく、なぜコードが動作していないのかを知る方法がありません。明らかにそれは何かしているべきではありませんが、何を知るかは分かりません。 – Servy

答えて

0

これは私が非同期呼び出しで問題に取り組む方法です。
コンテキスト:
ここで私はWPF

using System.Threading; 
using System.Threading.Tasks; 
using System.Windows.Data; 
using System.Windows.Input; 
using System.Windows.Threading; 
class VM 
    { 
     Dispatcher _dispatcher = Dispatcher.CurrentDispatcher; 
     //Time consuming operation 
     private void LongTask() 
     { 
      Thread.Sleep(5000); 
      //in here if you need to send something to the UI thread like an event use it like so: 
      _dispatcher.Invoke(new Action(() => 
      { 
       //some code here to invoke an event 
       if (ComponentsLoaded != null) 
        ComponentsLoaded(this, new EventArgs { }); 
      })); 
     } 

     private ICommand _command; 
     //This is the command to be used instead of click event handler 
     public ICommand Command 
     { 
      get { return _command; } 
      private set { _command = value; } 
     } 
     //method associated with ICommand 
     void commandMethod(object parameter) 
     { 
      Busy = true; 
      ThreadPool.QueueUserWorkItem(new WaitCallback(multiThreadTask)); 
      Busy = false; 
     } 
     //the task to be started on another thread 
     void multiThreadTask(object parameter) 
     { 
      LongTask(); 
     } 

     public event EventHandler ComponentsLoaded; 
    } 

で作業する場合これは、WPFで複数のスレッドで作業するときに私が使用するものですあなたに良い練習を表示するためにMvvMを使用しています。
これをコードビハインドで使用しても、Dispatcherのインスタンスを作成するだけで十分です。
さらに詳しい情報が必要な場合は、私たちにお知らせください。 HTH

関連する問題