2012-03-03 37 views
0

我已经搜索了关于如何修复 下面的问题的示例,但似乎无法找到我所需要的,并开始到 驱动我便盆!线程问题 - web服务完成之前的UI更新

从本质上讲,我有一个竞赛条件,即web服务在返回给用户界面后正在完成方式 ,所以UI根本不显示任何东西 。

这是代码。 web服务确实得到了正确的数据,这是所有的痛苦! generateNewScreen方法是由于 单击ListView区域上的一些文本而产生的。

调用程序

private void generateNewScreen(int t) 
{ 
    string[] races = new string[] { }; 
    View currentview = FindViewById<View>(Resource.Id.relLayout); 
    TextView text = FindViewById<TextView>(Resource.Id.textTitle); 
    ListView listView = FindViewById<ListView>(Resource.Id.listView); 
    ImageView image = FindViewById<ImageView>(Resource.Id.imgBack); 
    image.Visibility = ViewStates.Visible; 

    Console.WriteLine("t = {0}, addFactor = {1}", t,addFactor); 

    switch (addFactor) 
    { 
     case 0: 
      switch (t) 
      { 
       case 0: races =listviewInfo(Resource.Array.RaceTracks, 
        Resource.Drawable.Back_RaceHorsePlace, Resource.String.Tracks); 
        addFactor = 10; 
        break; 

       case 1: List<string> 
        race = new List<string>();             
        currentview.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.Back_BobMoore)); 
        text.Text = Resources.GetString(Resource.String.ComingSoon); 
        webservice_user getRace = new webservice_user(); 
        race = getRace.getUpcomingRaces("RP"); 
        races = race.ToArray(); 
        addFactor = 20; 
        break; 
      } 

      if (t < 6 || t == 7) 
       listView.Adapter = new ArrayAdapter<string>(this, Resource.Layout.listview_layout, races); 
      break; 
    } 
} 

Webservice的

private string rTrack; 

public List<string> getUpcomingRaces(string track) 
{ 
    List<string> f = new List<string>(); 
    rTrack = track; 
    getUpcomingRacesCallBack((list) => 
    { 
     f = list; 
    }); 
    return f; 
} 

private void getUpcomingRacesCallBack(Action<List&lt;string>> callback) 
{ 
    List<string> f = new List<string>(); 

    if (checkForNetwork(true) != true) 
    { 
     f.Add("No network available"); 
     callback(f); 
    } 
    else 
    { 
     List<POHWS.webservice.UpcomingRaces> tableData = new List<POHWS.webservice.UpcomingRaces>(); 
     POHWS.webservice.Service1 Service3 = new POHWS.webservice.Service1(); 
     try 
     { 
      Service3.BeginGetUpcomingRacesList(rTrack, delegate(IAsyncResult iar) 
      { 
       tableData = Service3.EndGetUpcomingRacesList(iar).ToList(); 
       Android.App.Application.SynchronizationContext.Post(delegate 
       { 
        if (tableData.Count == 0) 
        { 
         f.Add("No Upcoming Races Found within the next 7 days"); 
         callback(f); 
        } 
        else 
        { 
         for (int i = 0; i < tableData.Count;++i) 
          f.Add(tableData[i].PostTime); 
         callback(f); 
        } 
       }, null); 
      }, null); 
     } 
     catch (Exception oe) 
     { 
      f.Add(oe.ToString()); 
      callback(f); 
     } 
    } 
} 

是否有可能是停止的UI或延迟更新,直到Web服务 做了什么工作需要?我已经尝试了很多东西,但没有提供。

回答

1

这里的问题是getUpcomingRaces()的行为就好像对getUpcomingRacesCallBack()的调用是同步的,并立即返回列表。由于在返回语句之前lambda不太可能被触发,所以它总是返回空列表。

我会建议重新构建代码,以便它只在返回后才会在列表上执行,类似于您使用getUpcomingRacesCallBack()方法执行的方法,该方法需要Action<List<string>>

如果有帮助,我有一个示例项目available here,显示如何使用此模式。我也有一个post here,讲述了一些不同的方法来完成UI线程的工作,以防万一你最终走上使调用同步的路线。