Asp.net果然可以异步处理http请求

这是我的一大发现。。(虽然很有可能早就有别人发现了。。)

我这里的异步是指,客户端1某此请求服务器端数据,服务器端先进行数据的接收,直到某一条件(比如说有另外的客户端2访问、某事件的触发)时,继续进行客户端1的数据的发送。

这里以客户端2的访问为例。

在aspx网页中放上一个Button,代码如下:

C#语言:
namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["thread"] != null)
            {
                System.Threading.Thread th=Application["thread"] as System.Threading.Thread;
                if ((th.ThreadState & System.Threading.ThreadState.Suspended) > 0)
                {
                    HttpContext hc = Application["context"] as HttpContext;
                    hc.Response.Write("hello world!");
                    th.Resume();
                   
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Application["thread"] = System.Threading.Thread.CurrentThread;
            Application["context"] = HttpContext.Current;
            System.Threading.Thread.CurrentThread.Suspend();
            Response.End();
        }
       
    }
}

某个客户端1访问时,按下按钮,然后客户端1处于等待状态;

直到另外一个客户端2访问时,客户端1的服务器处理线程停止挂起状态,并且客户端1的输出交给了服务器处理客户端2时的线程。

这正是我想要的效果。Asp.net的Application真是强大啊。。

Anran进行回复 取消回复