2009-09-10 85 views
0

我正在调用并从异步Web服务调用中获取结果,该调用提供了一组对象,以便在手持式Windows Mobile的网格中显示。Windows Mobile 6.1异步Web服务调用问题和更新UI

有一次我使用InvokedRequired和Invoke正确更新了UI。现在,我第一次运行模拟器和Visual Studio 2008时,它将工作,但随后的调用似乎挂在Invoke方法调用上,代码中没有其他断点。

这个应用程序使用.Net CF 2.0 SP1,并针对WinMo 6.1设备。我最近从运行XP的虚拟开发环境切换到运行Vista的主机笔记本电脑。

private delegate void UpdateGrid(WebServiceItems[] items); 

private void DoGridUpdate(WebServiceItems[] items) 
    { 
     // Choose the correct grid based on the tab index 
     DataGrid grid; 
     if (tabSelectedIndex == 0) 
      grid = gridA; 
     else 
      grid = gridB; 

     if (grid.InvokeRequired) 
     { 
      grid.Invoke(new UpdateGrid(DoGridUpdate), new object[] { items }); 

      return; 
     } 

     Cursor.Current = Cursors.Default; 
     grid.DataSource = items; 
     if (items.Length > 0) 
     { 
      DataGridTableStyle tableStyle = new DataGridTableStyle(); 
      tableStyle.MappingName = items.GetType().Name; 

      DataGridTextBoxColumn column = new DataGridTextBoxColumn(); 
      column.Width = 230; 
      column.MappingName = "Column1"; 
      column.HeaderText = "Column1"; 
      tableStyle.GridColumnStyles.Add(column); 

      column = new DataGridTextBoxColumn(); 
      column.Width = 70; 
      column.MappingName = "Column2"; 
      column.HeaderText = "Column2"; 
      tableStyle.GridColumnStyles.Add(column); 

      grid.TableStyles.Clear(); 
      grid.TableStyles.Add(tableStyle); 
     } 
    } 
+0

我把if代码改成了这个。它曾经工作过,所以我停止了调试,重新启动,现在它被卡在!result.IsCompleted循环中。如果(this.InvokeRequired) IAsyncResult result = this.BeginInvoke(new UpdateGrid(DoGridUpdate),new object [] {items});如果(this.InvokeRequired) IAsyncResult result = this.BeginInvoke while(!result.IsCompleted) System.Threading.Thread.Sleep(100); this.EndInvoke(result); return; } – Brian 2009-09-10 20:29:36

回答

1

我忘了这个问题,但找到了一个解决方案。我来自ASP.NET背景,当时并没有意识到保持服务的范围,我很惊讶它的工作。

该服务的声明在按钮onclick事件中,所以我将它移动到窗体构造函数中,并为窗体创建服务全局,以便所有方法都可以访问它,现在一切都按预期工作。