Windows Phone 7 threading problem
I'm developing a twitter application for Windows Phone 7 and have been doing some prototyping. I have come across a problem when minipulating so开发者_Go百科me objects on the application. The below code just hangs (nothing moves and the search doesn't update) unless I comment out Dispatcher.BeginInvoke(new ThreadStart (doProgressBar));
I have also tried using a background worker thread to run doProgressBar()
but with the same effect. Sticking a breakpoint in shows that it does hit doProgressBar()
and that seems to be working correctly.
Any help helping me understand what's wrong would be appreciated. I don't see why I don't at least see the progress bar update. I'm also really confused why the main thread would hang when something is running in another thread. Possibly there is something about threading in a mobile application I need to understand?
public MainPage()
{
InitializeComponent();
}
private void ProcessIncommingSearch(TwitterSearchResult searchResult, TwitterResponse response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
foreach (var status in searchResult.Statuses)
{
TwitterStatus inline = status;
Tweet tweet = new Tweet(inline);
Dispatcher.BeginInvoke(() => lboxTweets.Items.Add(tweet));
}
}
}
private void image1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
Dispatcher.BeginInvoke(() => lboxTweets.Items.Clear());
Dispatcher.BeginInvoke(new ThreadStart (doProgressBar));
TwitterService ts = GlobalVariables.Service();
ts.Search(txtSearch.Text, ProcessIncommingSearch);
}
private void doProgressBar()
{
while (progressBar1.Value <= 100)
{
progressBar1.Value += 1;
}
}
private void txtSearch_GotFocus(object sender, RoutedEventArgs e)
{
txtSearch.Text = "";
}
private void txtPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
throw new NotImplementedException();
}
I'm having a little difficulty understanding your code here, but when you call Dispatcher.BeginInvoke(new ThreadStart (doProgressBar));
it looks like you are invoking a new thread from the UI thread. That means that doProgressBar
actually isn't running on the UI thread, so you probably won't see any updates to it until after it completes and the UI thread triggers an update.
Also, your code will just spin from progressBar.Value to a hundred in record time, possibly faster than you would be able to see it update.
Also, you mention commenting out Dispatcher.BeginInvoke(() => doProgressBar());
-- I don't see this anywhere in your code. Are you sure you posted the right version?
精彩评论