首页 新闻 赞助 找找看

C# 网页中的异步是不是只能用ajax来请求啊?我用BeginInvoke这种异步请求会出错..

0
[已解决问题] 解决于 2012-11-07 10:49

我用异步请求的代码如下,运行的时候会出错,怎么写才正确呢?

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来请求的呢?

谢谢指导!

hexllo的主页 hexllo | 菜鸟二级 | 园豆:318
提问于:2012-09-17 10:42
< >
分享
最佳答案
1

你写的那个是在服务器端的异步操作,我们一边说的异步,都是指的客户端通过ajax,异步的去取得服务器端的数据,也就是指的,页面不刷新

奖励园豆:5
chenping2008 | 大侠五级 |园豆:9836 | 2012-09-17 10:51
其他回答(1)
0
 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;
Pretty_Cool | 园豆:157 (初学一级) | 2012-09-18 14:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册