I'm trying to use TweetSharp from a static class (called TitterHandler
) so theoretically I can swap it out later.
The example suggests using Dispatch.BeginInvoke
so when the UI is updated it is done using the UI thread.
I would like to just load data into the model, although admittedly this will trigger an OnPropertyChanged
event which will update the UI. Since this is from a static method I use System.Windows.Deployment.Current.Dispatcher.BeginInvoke
.
However it keeps crashing with an 'Object not set to a reference ' exception. I have fairly limited understanding of threads but from what I gather
- The UI thread needs to update the UI but the main thread may fire an event which is then picked up by the UI thread - to test this I assign to a static variable totally separate from the UI (
TweetCache
) but the app still crashes - The thread may be garbage collected if there are no references to it. I placed a reference (
Thread
) on the class but still it crashes.
I am out of ideas, here is the code ...
public class TwitterHandler
{
public static TwitterService Service = new TwitterService(AppConfig.TwitterConsumerKey, AppConfig.TwitterConsumerSecret);
internal static void LoadTweetsFromWeb()
{
TwitterHandler.Service.AuthenticateWith(AppConfig.TwitterToken, AppConfig.TwitterTokenSecret);
TwitterHandler.Service.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions()
{ ScreenName = AppConfig.NewsBrandingTwitterHandle, },
TwitterHandler.OnTweetsLoaded);
}
// Put this here to maintain a refrence to the thread to avoid GC
public static DispatcherOperation Thread;
// A 'thread external' reference with no onward events that trigger UI changes
public static List<Tweet> TweetCache;
public static void OnTweetsLoaded(IEnumerable<TwitterStatus> statuses, TwitterResponse res)
{
if (res.StatusCode == HttpStatusCode.OK)
{
TwitterHandler.Thread = System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
List<Tweet> tweets = new List<Tweet>();
foreach (TwitterStatus status in statuses)
{
tweets.Add(new Tweet()
{
Content = status.Text,
UrlString = status.Place.Url,
});
}
if (tweets.Count > 0)
{
// App.Tweets = tweets;
// TweetCache raises no 'PropertyChanged' event
TwitterHandler.TweetCache = tweets;
}
});
}
}
}