2013-03-12 79 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