我有我在一個新的任務與WPF線程延遲
// get the dispatcher for the UI thread
var uiDispatcher = Dispatcher.CurrentDispatcher;
Task.Factory.StartNew(() => BackgroundThreadProc(uiDispatcher));
調用該方法BackgroundThreadProc()
我需要幾秒鐘的延遲的方法。我用DispatcherTimer和task.delay函數嘗試了它,但它不起作用。唯一的工作是System.Threading.Thread.Sleep(1)
,但我認爲Thread.Sleep()
功能不是最好的解決方案。
這是我的函數:
public void BackgroundThreadProc(Dispatcher uiDispatcher)
{
for (var i = 0; i < 100; i++)
{
var task = Task.Delay(1000).ContinueWith(t =>
{
// create object
var animal = new Animal { Name = "test" + i };
uiDispatcher.Invoke(new Action(() => log(animal)));
});
}
}
當我發現了,因爲DispatcherTimer在UI線程中運行它沒有工作。我如何才能在UI線程之外的其他線程中實現延遲?
更新:
現在我與定時器試了一下:
public void BackgroundThreadProc(Dispatcher uiDispatcher)
{
for (var i = 0; i < 100; i++)
{
var _delayTimer = new System.Timers.Timer();
_delayTimer.Interval = 1000;
//_delayTimer.Enabled = true;
_delayTimer.Elapsed += delegate
{
var animal = new Animal { Name = "test" + i };
uiDispatcher.Invoke(new Action(() => log(animal)));
_delayTimer.Stop();
};
_delayTimer.Start();
}
}
Hy,謝謝ypur的回答。我試着用你的例子,但問題是延遲結束後,它一直寫「test100」到我的輸出,因爲在延遲後,for循環已經完成,但我希望在每個I ++之後延遲。我將BackgroundThreadProc()添加到了我的文章中。 – user2644964
@ user2644964聽起來就像你只是想要一個'計時器'。你可以使用TPL來做到這一點,但它的工作方式比它的價值更大。只需使用一個「計時器」。 – Servy
Hy,帶計時器,你的意思是System.Timers.Timer?我嘗試了一個例子,並更新了「更新」下的主要帖子,你可以找到我嘗試過的功能。我最終遇到同樣的問題,它只在輸出上寫了一個houndret時間「test100」。 – user2644964