2013-01-23 43 views
0

在我的表單加載事件中,我從數據庫中加載了一些東西,並向數據庫以及其他服務發送了一些更新。有了這些更新,我一直在使用Task.Run((=)=>來異步運行這些更新。在我看來,我只想使用異步/等待,當我仍想異步執行更新時,仍然按順序通過我的表單的加載事件。在下面的例子中,我不關心在我點擊doOtherWork之前setupDBUpdate或sendServiceCall完成。我應該在Task.Run中使用異步/等待

void MainForm_Load(object sender, EventArgs e) 
{ 
    loadData(); 
    setupDBUpdate(); 
    sendServiceCall(); 
    doOtherWork(); 
} 

private void setupDBUpdate() 
{ 
    var timer = new Timer(); 
    timer.Interval = 60000; 
    timer.Tick += timer_Tick; 
    timer.Start(); 
} 

void timer_Tick(object sender, EventArgs e) 
{ 
    Task.Run(() => 
    { 
    // do database update 
    }); 
} 

void sendServiceCall() 
{ 
    Task.Run(() => 
    { 
    // connect to service and send an update. 
    }); 
} 

回答

0

只要你有一定的途徑,以確保您嘗試處理數據之前,你的任務都做了,一切都應該是OK。