हमारे पास डब्ल्यूपीएफ में एक एप्लीकेशन है जो ऑब्जर्जेबल कोलेक्शन के माध्यम से डेटा दिखाता है। 5 मिनट के बाद, मैं डेटा रीफ्रेश करना चाहता हूं।डब्ल्यूपीएफ में टाइमर के साथ यूआई रीफ्रेश करें (पृष्ठभूमि वर्कर के साथ?)
मुझे लगता है मैं अपने Elapsed
घटना के लिए System.Timers.Timer
वस्तु इस्तेमाल कर सकते हैं और फिर कहते हैं एक BackgroundWorker
विधि है कि काम शुरू होता है कॉल करने के लिए सोचा। विधि ViewModel क्लास पर है।
लेकिन ऐसा लगता है कि धागे की समस्या है।
तो मैंने डिस्पैचर के साथ प्रयास किया, लेकिन फिर भी वही बात।
यहाँ मेरी (सरलीकृत और अनुकूल नहीं) कोड है:
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationController"/> class.
/// </summary>
public ApplicationController()
{
CreateDefaultTabs();
Timer timer = new Timer(20000); //20 secs for testing purpose.
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(OnTimeBeforeRefreshElapsed);
timer.Start();
}
private void OnTimeBeforeRefreshElapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.CurrentDispatcher.Invoke(new Action(() => { RefreshData(); }));
Dispatcher.CurrentDispatcher.Invoke(new Action(() => { UpdateLayout(); }));
}
private void RefreshData()
{
foreach (object tab in _tabItems)
{
if (tab is TitleDetailsView)
{
TitleDetailsViewModel vm = ((TitleDetailsView)tab).DataContext as TitleDetailsViewModel;
vm.Refresh();
}
}
}
private void UpdateLayout()
{
foreach (object tab in _tabItems)
{
if (tab is TitleDetailsView)
{
TitleDetailsViewModel vm = ((TitleDetailsView)tab).DataContext as TitleDetailsViewModel;
vm.HandleGetTitleBySymbolResponse();
}
}
}
मैं कैसे आगे बढ़ना चाहिए पर कोई सुझाव?
धागे के साथ एक समस्या है? वास्तव में क्या समस्या है? –