我正在開發一個應用程序,它加載一些url,解析它們,將它們保存到sqlite數據庫中,UI將讀取保存的數據並在控件中顯示它們。這一進程應該在幾乎無窮循環中完成。爲了快速響應,我打算從主線程中的數據庫中讀取數據,並讓其他線程(後臺工作者)加載數據並將其插入數據庫。在dispatchertimer中運行讀取和寫入過程是否合乎邏輯並且可能?在主線程中運行一個計時器,而在後臺工作者中運行另一個計時器?如何?或者有沒有人有更好的主意?在Windows Phone的後臺工作人員使用計時器
main thread:
DispatcherTimer _Timer1 = new DispatcherTimer();
_Timer1.Interval = _Interval;
_Timer1.Tick += _Timer1_Tick;
void _Timer1_Tick(object sender, EventArgs e)
{
// read data from db and show in controls
}
secondary thread:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
DispatcherTimer _Timer2 = new DispatcherTimer();
_Timer2.Interval = _Interval;
_Timer2.Tick += _Timer2_Tick;
}
void _Timer2_Tick(object sender, EventArgs e)
{
// write data into db
}
}
你有工作的樣本嗎?只是爲了正確地知道問題。 – 2013-02-11 13:32:25
@Doc Roms:我已經編輯了這個問題,現在你可能會明白我在說什麼。這聽起來合乎邏輯嗎?有沒有更好的辦法? – Finutv 2013-02-11 13:53:50
我想頻繁地運行db中的讀寫操作,並能夠在第一次運行後停止定時器或者在間隔時間內改變 – Finutv 2013-02-11 13:56:21