如何在ASP.NET页面中实现异步加载?

发布于 2024-12-25 23:33:23 字数 971 浏览 0 评论 0原文

如何在ASP.NET页面中实现异步加载?

我的 ASP.NET 页面中有 3 个部分。所有部分都是独立的。

LoadSection1();
LoadSection2();
LoadSection3();

每个部分大约需要 15 秒。我想使用异步加载来减少页面加载时间。

我尝试过线程

// Create thread jobs for each section
ThreadStart PipelinePerformanceThreadJob = new ThreadStart(LoadPipelineSection);
ThreadStart CampaignPerformanceThreadJob = new ThreadStart(LoadCampaignSection);
ThreadStart OperationalThreadJob = new ThreadStart(LoadOperationalSection);

// Create threads
Thread PPThread = new Thread(PipelinePerformanceThreadJob);
Thread CSThread = new Thread(CampaignPerformanceThreadJob);
Thread OSThread = new Thread(OperationalThreadJob);

// Start all the threads
PPThread.Start();
CSThread.Start();
OSThread.Start();

// Join threads with main thread
PPThread.Join();
CSThread.Join();
OSThread.Join();

页面一旦完成所有线程就加载。但每当我收到线程的响应时,我都需要显示每个部分的数据。 例如,如果线程 1 已完成,我想显示部分 1 的数据(即使线程 2 和 3 仍在运行)。我怎样才能在.NET 中实现这一点?

How to implement asynchronous loading in ASP.NET page?

I have 3 sections in my ASP.NET page. All the sections are independent.

LoadSection1();
LoadSection2();
LoadSection3();

Each section is taking around 15 seconds. I want to reduce the page load time using asynchronous loading.

I have tried with Threads

// Create thread jobs for each section
ThreadStart PipelinePerformanceThreadJob = new ThreadStart(LoadPipelineSection);
ThreadStart CampaignPerformanceThreadJob = new ThreadStart(LoadCampaignSection);
ThreadStart OperationalThreadJob = new ThreadStart(LoadOperationalSection);

// Create threads
Thread PPThread = new Thread(PipelinePerformanceThreadJob);
Thread CSThread = new Thread(CampaignPerformanceThreadJob);
Thread OSThread = new Thread(OperationalThreadJob);

// Start all the threads
PPThread.Start();
CSThread.Start();
OSThread.Start();

// Join threads with main thread
PPThread.Join();
CSThread.Join();
OSThread.Join();

Page is loading once it completed all the threads. But i need to display data for each section whenever i get response from the thread.
For e.g. If Thread1 is completed, i want to display data for Section1 (even if thread2 and 3 are still running.). How can i achieve this in .NET?

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

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

发布评论

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

评论(2

幽梦紫曦~ 2025-01-01 23:33:24

我会考虑使用 AJAX 以便用户立即看到一些内容。

I would consider using AJAX so that the user sees something right away.

冷月断魂刀 2025-01-01 23:33:24

我认为您可以尝试一些相对简单的选项,具体取决于页面的复杂性和错综复杂性(即是否是静态数据,是否所有内容都必须回发等):

1)将页面分成三个不同的页面,然后将框架或 iframe 添加到原始页面,并将框架定向到新页面。

2) 将页面分成三个用户控件,并将它们添加到更新面板内的页面。

在控件的代码隐藏中,不要在初始页面加载时执行加载操作(即不回发)。

要加载控件内容,请将 JavaScript 添加到 window.onload 事件或 (page.registerstartupscript) 中,该事件执行一些简单的操作,例如单击 updatepanel 中的按钮,以强制回发。但是,请确保在每个用户控件的 window.settimeout 中执行此操作,这样您就不必等待第一个控件完成。

这将启动用户控件的异步加载。

I think that you have a couple of relatively easy options that you can try, depending on the complexity and intricacy of your page (i.e. is it static data, does everything have to post back etc):

1) Separate your page into three different pages, then add frames or iframes to your original page and direct the frames to your new pages.

2) Separate your page into three user controls and add them to the page within update panels.

In the control's codebehind, do not perform the load action on the initial page load (i.e. not postback).

To load the control content, add javascript to the window.onload event or (page.registerstartupscript) that does something simple like clicks a button in the updatepanel, to force a postback. However, ensure that you do this in a window.settimeout for each user control so that you don't have to wait for the first one to complete.

This will kick off the async loading of the user controls.

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