繁体   English   中英

如何在1个阻塞呼叫中执行操作?

[英]How to perform the operation in 1 blocking call?

如何将这2个操作变成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)
            {
            }
        }

在99.9%的情况下,将异步调用转换为阻塞调用是个坏主意,因此我很想知道您为什么需要这么做。

如果从不执行第二项操作,则可能存在服务器端或连接问题。 假定您正在使用WCF服务,请使用WCF测试客户端检查返回的结果是否符合您的预期。

据我所知, NotifyComplete是在触发回调之前调用的,这很可能是问题所在。 当所有其他呼叫都通过时,存在潜在的竞争状况,其中回调的速度不够快,无法在将代理执行完成信号通知操作系统之前触发。

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

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

test.ReadTotalOutstandingInvoiceAsync();

从那里继续前进,为什么不让它返回一个int开头,这样您就不必解析它了?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM