2015-04-08 44 views
1

我有一個問題,我使用Application.Current名稱空間的Dispatcher類運行我的部分代碼。然後我想使用由Dispatcher.Invoke()返回的任務的ContinueWith方法構建一個後續操作。Application.Current.Dispatcher.Invoke()從一個動作

在這種特殊情況下,後續操作需要在UI線程中運行,因此需要再次將其包裝在Dispatcher.Invoke中。這是我得到它的工作:

Action doSomeMoreStuff =() => { this.MoreStuff; } 
Application.Current.Dispatcher.Invoke(() => this.DoStuff).ContinueWith(x => Application.Current.Dispatcher.Invoke(this.DoSomeMoreStuff)); 

我想保留它通用不過了,有可能是在那裏我不想跟進代碼從UI線程中運行的情況。於是,我就封裝跟進代碼本身:

Action doSomeMoreStuff =() => { Application.Current.Dispatcher.Invoke(this.MoreStuff); } 
Application.Current.Dispatcher.Invoke(() => this.DoStuff).ContinueWith(x => this.DoSomeMoreStuff); 

所以,據我理解這個問題,我簡單地切換的Application.Current.Dispatcher.Invoke()調用的位置。但是,第二種方法不起作用,代碼不會被調用,我不知道爲什麼。

我不在這裏?

+3

使用異步/等待。 –

+0

你可以更具體一點,即發表一個例子嗎? – Slicky

+0

[這裏](http://tinyurl.com/mzlkyl7)。 –

回答

1

我設法解決我的問題,開水ContinueWith()。它的工作原理

Action followUpAction =() => { }; 
Application.Current.Dispatcher.Invoke(
       async() => 
        { 
         await this.DoStuff() 

         followUpAction(); 
        }); 

感謝您的有益評論。

相關問題