2011-03-12 148 views
1

我正在开发Windows Phone 7的twitter应用程序,并且一直在做一些原型设计。在应用程序中缩小某些对象时遇到了问题。下面的代码只是挂起(没有动作,搜索不会更新),除非我注释掉Dispatcher.BeginInvoke(new ThreadStart (doProgressBar));我也尝试使用后台工作线程运行doProgressBar(),但效果相同。坚持一个断点表明它确实击中了doProgressBar(),这似乎是正常工作。Windows Phone 7线程问题

任何帮助我了解什么是错的将不胜感激。我不明白为什么我至少看不到进度条更新。我也很困惑为什么主线程在另一个线程中运行时会挂起。可能有一些关于我需要了解的移动应用程序中的线程?

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(); 
    } 
+0

您是否使用silverlight工具包中的常规进度条或性能进度条?它是什么TwitterService?自制或现有的图书馆? – 2011-03-12 12:27:50

+0

这里是进度条的xaml 。 Twitterservice来自tweetsharp库。它工作正常,没有进度条更新。 – 2011-03-12 17:23:20

回答

1

我在这里认识你的代码有点困难,但是当你调用Dispatcher.BeginInvoke(new ThreadStart (doProgressBar));看起来你是从UI线程调用一个新的线程。这意味着doProgressBar实际上并未在UI线程上运行,因此您可能直到它完成并且UI线程触发更新之后才会看到任何更新。

此外,您的代码将从progressBar.Value旋转到记录时间内的一百次,可能比您能够看到它更新更快。

此外,你提到注释出Dispatcher.BeginInvoke(() => doProgressBar()); - 我没有看到你的代码中的任何地方。你确定你发布了正确的版本吗?

+0

我已经更新了代码。我尝试了几种不同的方法调用该方法,并将错误的方法留下!它并不像我所说的那样完整。我不明白为什么。 – 2011-03-12 17:20:28