Reputation: 125
I am trying to call a method every second using DispatcherTimer(); this works fine when updating the timer, however whenever I try to update any UI Emements in the method the timer seems to tick every 2 seconds instead of every second and the more UI elements I add to update increases the tick time, also the window cant be dragged around the screen between the ticks.
How can I get the time to update every second correctly?
I'm querying a web api to receive data. I wonder could this be the problem?
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
var dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
// Updating the Label which displays the current second
lblTime.Content = DateTime.Now.ToString("HH:mm:ss");
lblSizeBTC_USD.Content = "SIZE " + Api.GetProductTicker(productId).Result.size;
// Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested();
}
Method calling the web api
public static async Task<ProductTicker> GetProductTicker(string productId)
{
string ts = GetNonce();
string method = "/products/" + productId + "/ticker";
string sig = GetSignature(ts, "GET", method, string.Empty);
ProductTicker productTicker;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("CB-ACCESS-KEY", apiKey);
client.DefaultRequestHeaders.Add("CB-ACCESS-SIGN", sig);
client.DefaultRequestHeaders.Add("CB-ACCESS-TIMESTAMP", ts);
client.DefaultRequestHeaders.Add("CB-ACCESS-PASSPHRASE", passphrase);
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
HttpResponseMessage response = client.GetAsync(method).Result;
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
productTicker = JsonConvert.DeserializeObject<ProductTicker>(json);
}
return productTicker;
}
Upvotes: 0
Views: 460
Reputation: 125
Clemens was indeed correct the working updated code is below. The solution was to make the DispatcherTimer_Tick
method async and to await the GetProductTicker
call:
private async void DispatcherTimer_Tick(object sender, EventArgs e)
{
// Updating the Label which displays the current second
lblTime.Content = DateTime.Now.ToString("HH:mm:ss");
var productTicker = await Api.GetProductTicker(productId);
lblSizeBTC_USD.Content = "SIZE " + productTicker.size;
// Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested();
}
Upvotes: 1