2016-10-04 46 views
0

我想更新函數開始處的忙指示符。 僅當功能完成時才執行綁定。綁定WPF不立即執行

private async void DoJob() 
{ 
    await Task.Run(() => SetBusyIndicatorState(true)); 

    var res = await (LongFunction()); 
    ... 

    await Task.Run(() => SetBusyIndicatorState(false)); 
} 

private void SetBusyIndicatorState(bool state) 
{ 
    Application.Current.Dispatcher.BeginInvoke(new Action(() => 
     { 
      RadBusyIndicatorLoad.IsBusy = state; 
     })); 
} 
+0

當我執行該功能我沒有看到繁忙指示符,因爲結合在端執行(等待Task.Run(()=> SetBusyIndi​​catorState(假));) – leapold

回答

0

將您的方法標記爲異步並等待執行完成。

 SetBusyIndicatorState(true); 

    Task.Run(() => 
    { 
     //implement 
    }).ContinueWith(completedtaskresult=> 
    { 
     SetBusyIndicatorState(false) 
    }, TaskScheduler.FromCurrentSynchronizationContext()); 
+0

我可以因爲我不接收消息(調用線程不能訪問這個對象,因爲不同的線程擁有它)。 LongFunction()內部運行其他任務 – leapold