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

Bin*_*ker 7 c# asp.net asynchronous

我们有服务将客户信息更新到服务器.一次服务呼叫大约需要几秒钟,这是正常的.

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

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

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

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

Joe*_*ham 8

详细信息:http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

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

// Declare the delegate - name it whatever you would like
public delegate void ProcessCustomerInfoDelegate();

// Instantiate the delegate and kick it off with BeginInvoke
ProcessCustomerInfoDelegate d = new ProcessCustomerInfoDelegate(ProcessCustomerInfo); 
simpleDelegate.BeginInvoke(null, null);

// The method which will run Asynchronously
void ProcessCustomerInfo()
{
   // this is where you can call your webservice 50 times
}
Run Code Online (Sandbox Code Playgroud)

  • 关于在没有EndInvoke的情况下调用BeginInvoke是否是一个坏主意已经有了一些讨论 - 主要是因为没有在某些委托上调用EndInvoke操作会导致内存泄漏.可能我建议使用ThreadPool.QueueUserWorkItem(cb => ProcessCustomerInfo()) (2认同)