异步调用开发应用总结

异步调用的一些开发应用总结:

WEB应用需要异步调用,保证在一些比较耗时的功能执行过程同时不阻塞主线程执行,尤其是WCF服务调用,使用异步调用还是很有必要的。

关于异步调用,分别总结下方法的异步调用与WCF服务的异步调用。

方法异步调用与同步调用示例:

 public string TestMethod(string Code, object userState)
        {

//同步
GetName(Code);

//异步
            try
            {
                AsyncGetName asyncDelegateGetName = delegate(string Code_bp)
                {
                    return this.Command.GetName(Code_bp);
                };
                IAsyncResult iaResult = asyncDelegateGetName.BeginInvoke(Code, delegate(IAsyncResult resultCallBack)
                {
                    System.Runtime.Remoting.Messaging.AsyncResult result = (System.Runtime.Remoting.Messaging.AsyncResult)resultCallBack;
                    AsyncGetName asyncDelegate = (AsyncGetName)result.AsyncDelegate;
                    if (result.IsCompleted)
                    {
                        string asyncResult = asyncDelegate.EndInvoke(resultCallBack);
                        this.GetNameCompleted(this, new INewTestManage_GetNameCompletedEventArgs(new object[] { asyncResult }, null, false, result.AsyncState));
                    }

                }, userState);
            }
            catch
            {
                throw;
            }
        }

 

WCF服务异步调用,待续。。

 

 

posted @ 2013-08-22 18:11  longxc  阅读(218)  评论(0编辑  收藏  举报