带有 Action.BeginInvoke 的 ASP.Net 异步页面

发布于 2024-12-08 16:53:27 字数 1051 浏览 2 评论 0原文

我正在尝试在 asp .net 中编写一个异步页面,该页面在不同的线程中运行一个函数。 以下代码中的问题是,当我调试它时,函数 EndAsyncOperation 永远不会被调用。因此,页面不会完全加载并永远加载。 我使用 Action 在线程池中的不同线程中运行代码。是否有另一种方法可以在不同的线程中运行代码?

我哪里错了?

还有一个问题。我读到,在 ASP .Net 中,页面是使用线程池运行的。那么当我调试我的网站并尝试一起加载几个页面时,为什么它们会一个接一个地同步加载呢?

public partial class AsyncPage : System.Web.UI.Page
{
    void Page_Load(object sender, EventArgs e)
    {
        AddOnPreRenderCompleteAsync(
            new BeginEventHandler(BeginAsyncOperation),
            new EndEventHandler(EndAsyncOperation)
        );
    }

    IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
        AsyncCallback cb, object state)
    {
        Action action = () =>
            {
                Start();
            };

        IAsyncResult asyncResult = action.BeginInvoke(new AsyncCallback(action.EndInvoke), null);

        return asyncResult;
    }

    void EndAsyncOperation(IAsyncResult ar)
    {
        // This function isn't reached
    }

    public void Start()
    {
        // Do something
    }
}

I'm trying to write an async page in asp .net which runs a function in a different thread.
The problem in the following code, is that when I debug it, the function EndAsyncOperation is never called. As a result, the page isn't fully loaded and loads for ever.
I use Action to run the code in a different thread from the thread pool. Is there maybe another way of running the code in a different thread that works?

Where am I going wrong?

And another question. I read that in ASP .Net the pages are ran with a threadpool. So howcome when I debug my site and try to load a few pages together they are loaded one after another syncronously?

public partial class AsyncPage : System.Web.UI.Page
{
    void Page_Load(object sender, EventArgs e)
    {
        AddOnPreRenderCompleteAsync(
            new BeginEventHandler(BeginAsyncOperation),
            new EndEventHandler(EndAsyncOperation)
        );
    }

    IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
        AsyncCallback cb, object state)
    {
        Action action = () =>
            {
                Start();
            };

        IAsyncResult asyncResult = action.BeginInvoke(new AsyncCallback(action.EndInvoke), null);

        return asyncResult;
    }

    void EndAsyncOperation(IAsyncResult ar)
    {
        // This function isn't reached
    }

    public void Start()
    {
        // Do something
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-12-15 16:53:27

我相信您需要将 BeginAsyncOperation 方法参数中提供给您的 AsyncCallback 对象传递给 BeginInvoke,而不是创建一个新的。

由于您的会话配置,您的页面正在同步加载。

对 ASP.NET 会话状态的访问是每个会话独占的,这意味着如果两个不同的用户并发请求,则同时授予对每个单独会话的访问权限。 但是,如果对同一会话发出两个并发请求(通过使用相同的 SessionID 值),则第一个请求将独占访问会话信息。第二个请求只有在第一个请求完成后才会执行。(如果由于第一个请求超过锁定超时而释放了信息上的独占锁,则第二个会话也可以获得访问权限。)如果如果@Page指令设置为ReadOnly,则对只读会话信息的请求不会导致对会话数据的独占锁定。但是,对会话数据的只读请求可能仍需要等待对会话数据的读写请求设置的锁被清除。

来源:ASP.NET 会话状态概述,我的重点。

I believe that you need to pass the AsyncCallback object provided to you in the BeginAsyncOperation method parameters to BeginInvoke, not create a new one.

Your pages are loading synchronously because of your session configuration.

Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. (The second session can also get access if the exclusive lock on the information is freed because the first request exceeds the lock time-out.) If the EnableSessionState value in the @ Page directive is set to ReadOnly, a request for the read-only session information does not result in an exclusive lock on the session data. However, read-only requests for session data might still have to wait for a lock set by a read-write request for session data to clear.

Source: ASP.NET Session State Overview, my highlight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文