2013-03-12 55 views
0

如何执行这2个操作成为1操作?其实我在后台代理中使用,但第二个操作不能执行,不能得到的价值。任何想法?如何在1个阻塞呼叫中执行操作?

public void running() 
{  
    ServiceReference1.WebServiceSoapClient test = new ServiceReference1.WebServiceSoapClient(); 

    test.ReadTotalOutstandingInvoiceCompleted += new EventHandler<ServiceReference1.ReadTotalOutstandingInvoiceCompletedEventArgs>(serviceClient); 

    test.ReadTotalOutstandingInvoiceAsync(); 
} 

public void serviceClient(object sender, ReadTotalOutstandingInvoiceCompletedEventArgs e) 
{ 

    answer = int.parse(e.Result.ToString()); 
} 

更新: 在后台代理:

 VibrateController testVibrateControl = VibrateController.Default; 
     public int answer; 


     protected override void OnInvoke(ScheduledTask task) 
     { 
      //TODO: Add code to perform your task in background 
      running(); 

      ShellTile t = ShellTile.ActiveTiles.First(); 

      StandardTileData d = new StandardTileData() 
      { 
       Title = "U have " + answer + " Invoice!!", 
       BackTitle = "How Are You!!", 
       //Count = 0, 
       // BackBackgroundImage = new Uri("dog.jpg", UriKind.Relative), 
       // BackgroundImage = new Uri("untitled.png", UriKind.Relative) 
      }; 
      t.Update(d); 

      testVibrateControl.Start(TimeSpan.FromSeconds(3)); 
      testVibrateControl.Stop(); 
      //ShellTile.Create(new Uri("/MainPage.xaml?pp=cas", UriKind.Relative), d); 
      NotifyComplete(); 
     } 

在主页:

 private void CheckBox_Checked_1(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       PeriodicTask p = new PeriodicTask("jj"); 
       p.Description = "Don't push the red button"; 
       ScheduledActionService.Add(p); 
       //ScheduledActionService.LaunchForTest(p.Name, TimeSpan.FromSeconds(2)); 
       #if (DEBUG_AGENT) 
       ScheduledActionService.LaunchForTest(p.Name, TimeSpan.FromSeconds(2)); 
       #endif 
      } 
      catch (Exception ex) 
      { 
      } 
     } 
+0

你能否提供一些背景知道为什么你想让它成为阻塞呼叫?它可以帮助人们选择可能的解决方案。 (如果不小心的话,它可能会导致界面无响应。) – lzcd 2013-03-12 00:33:30

回答

0

在的情况下,99.9%,这是一个坏主意,在变换的异步调用一个阻塞的,所以我会好奇听到你为什么需要这个。

如果第二个操作从未执行,则可能存在服务器端或连接问题。考虑到您使用的是WCF服务,请使用WCF Test Client来检查返回的结果是否符合您的期望。

从我所知道的,NotifyComplete在您的回调被触发之前调用,这很可能是问题所在。虽然所有其他调用都是通过的,但是有一种潜在的竞争条件,即在代理执行完成向OS发送信号之前,回调速度不够快而无法触发。

你可以做的是在内部包装回调。就像这样:

ServiceReference1.WebServiceSoapClient test = new ServiceReference1.WebServiceSoapClient(); 
test.ReadTotalOutstandingInvoiceCompleted += (s,e) => 
{ 
    // Do some things here 
    NotifyComplete(); 
}; 

test.ReadTotalOutstandingInvoiceAsync(); 

继续移动,为什么不把它返回一个int开始说起,这样你就不必解析出来?

+0

Thx @Den Delimarsky。如果我放在MainPage中,操作运行,我得到的数值,但如果我把后台代理程序,我不会得到的价值。怎么会这样? – likewer 2013-03-12 02:57:44

+0

编辑您的帖子,以显示您在后台代理中如何使用上述代码,以及如何安排代理。 – 2013-03-12 03:03:25

+0

我已更新我的帖子。谢谢你的帮助。 – likewer 2013-03-12 03:22:21