Updating Count Value of RadHubTile in using BackGroundWorker

While working on one of the application, I came across a requirement to keep updating count value of RadHubTile in Windows Phone. I had RadHubTile declared on XAML as following

 


<telerikPrimitives:RadHubTile Title="News"
ImageSource="Images\news.png"
Count="11"
Message="News Update"
Width="400"
x:Name="newshubtile">
<telerikPrimitives:RadHubTile.BackContent>
<Border Background="{StaticResource PhoneAccentBrush}">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="News mashup!!"/>
</Border>
</telerikPrimitives:RadHubTile.BackContent>
</telerikPrimitives:RadHubTile>

If you notice in above declaration, we have set value initial of Count Property to 11. Now we need to update value of count on different thread. We can do that using BackGroundWoker. Suppose we have function to update count value as following

 


private void increaseCountValue()
{

newshubtile.Count = newshubtile.Count + 2;

}

Now we need to execute this function in BackgroundWoker. In below function, we are passing two input parameters

  1. Function as Action
  2. Delay in msecond

We are creating instance of BackgroundWorker and calling the function asynchronously on different thread than UI thread.


private void UpdateUsingBackgroundWoker(Action updateMethod, int delayInMilliseconds)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, e) => Thread.Sleep(delayInMilliseconds);
worker.RunWorkerCompleted += (s, e) => updateMethod.Invoke();
worker.RunWorkerAsync();

}

Last but not least we need to call UpdateIUsingBackgroundEWorker as per our requirement. We can call this as following


this.UpdateUsingBackgroundWoker(() => increaseCountValue(), 1000);

This will update count value in delay of 1 second. In this way you can update count value. I hope you find this post useful. Thanks for reading!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s