I have a small application that checks some system status and display it details. I have the following code which is working. But I want to make sure it's optimal:
public partial class MainWindow : Window
{
// Dictionary to hold info
Dictionary<string, int> dataHolder = new Dictionary<string, string>
{
{"status_code", "" },
{"memory", "" },
{"process", "" }
};
public MainWindow()
{
InitializeComponent();
}
private void StartBtn_Click(object sender, RoutedEventArgs e)
{
StartBtn.IsEnabled = false;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(updateUI);
dispatcherTimer.Interval = new TimeSpan(0, 5, 0);
dispatcherTimer.Start();
}
private async void updateUI(object source, EventArgs e)
{
await loadInfo();
StartBtn.IsEnabled = true;
foreach(KeyValuePair<string, string> entry in dataHolder)
{
// Display in the UI
}
}
private async Task loadInfo()
{
int page = 1;
var keys = new List<string>(dataHolder.Keys);
foreach (string key in keys)
{
dataHolder[key] = "";
//await asyncCall(key, page);
}
// Better asynchronously and in parallel
await Task.WhenAll(keys.Select(key => asyncCall(key, page)));
}
private async Task asyncCall(string key, int page)
{
var htClient = new HttpClient();
// code to retrieve data...
}
}
So as I said before, this code is working but I read somewhere that having an async void
method is not good practice as in private async void updateUI(object source, EventArgs e)
. I could change it to private async Task updateUI(object source, EventArgs e)
but then I would not be able to do dispatcherTimer.Tick += new EventHandler(await updateUI);
Also how to start counting (Timer) only after the previous process is done?
Thank you
EDIT: I am using the DispatcherTimer
because I want the application to retrieve the status information and update the UI every 5 minutes.