c# – Fire和Forget(Asynch)ASP.NET方法调用

前端之家收集整理的这篇文章主要介绍了c# – Fire和Forget(Asynch)ASP.NET方法调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们有一项服务来更新客户信息到服务器.一次服务呼叫大约需要几秒钟,这是正常的.

现在我们有一个新页面,在一个实例中,可以更新大约35-50个Costumers信息.此时更改服务界面以接受所有客户是不可能的.

我需要调用一个方法(比如“ProcessCustomerInfo”),它将遍历客户信息并调用Web服务35-50次.异步调用服务并没有多大用处.

我需要异步调用方法“ProcessCustomerInfo”.我正在尝试使用RegisterAsyncTask. Web上有各种示例,但问题是在我离开此页面后启动此调用后,处理将停止.

是否可以实现Fire和Forget方法调用,以便用户可以从页面移开(重定向到另一个页面)而不停止方法处理?

解决方法

详情: http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

基本上,您可以创建一个委托,该委托指向您想要异步运行的方法,然后使用BeginInvoke将其启动.

  1. // Declare the delegate - name it whatever you would like
  2. public delegate void ProcessCustomerInfoDelegate();
  3.  
  4. // Instantiate the delegate and kick it off with BeginInvoke
  5. ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo);
  6. simpleDelegate.BeginInvoke(null,null);
  7.  
  8. // The method which will run Asynchronously
  9. void ProcessCustomerInfo()
  10. {
  11. // this is where you can call your webservice 50 times
  12. }

猜你在找的C#相关文章