我用异步请求的代码如下,运行的时候会出错,怎么写才正确呢?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Threading; using System.Runtime; namespace WebApplication2 { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Test t = new Test(T); t.BeginInvoke(new AsyncCallback((Ir) => {Response.Write("eeeeeeee"); }), null); Response.Write("dddddddd"); } private void T() { Thread.Sleep(10000); } delegate void Test(); } }
另外,在web编程中多线程是不是基本都用不上?都是用ajax来请求的呢?
谢谢指导!
你写的那个是在服务器端的异步操作,我们一边说的异步,都是指的客户端通过ajax,异步的去取得服务器端的数据,也就是指的,页面不刷新
1 Func<string, bool> caller = 2 p => 3 { 4 try 5 { 6 return SendPCMetaData(p); 7 } 8 catch (Exception ex) 9 { 10 if (_log.IsErrorEnabled) 11 _log.Error("SendPCMetaDataAsync,userId=" + userId, ex); 12 return false; 13 } 14 }; 15 16 var asyncResult = caller.BeginInvoke(userId, new AsyncCallback( 17 ar => 18 { 19 AsyncResult result = (AsyncResult)ar; 20 Func<string, bool> callerDelegate = (Func<string, bool>)result.AsyncDelegate; 21 var returnValue = callerDelegate.EndInvoke(ar); 22 }), null); 23 24 return true;