需要有关 C# 编码(效率)的建议
在我的代码中,我有一个下拉选择,从下拉列表中选择后,代码将执行进一步的处理并生成报告/数据。
此外,整个程序依赖于从 3 个不同操作收集的数据,
Operation1: processing a text files of size of size > 6MB
Operation2: SQL Query to a DB (Query takes around 1 minute)
Operation 3: HTTP POST request to server (The main costliest part of the programe)
因此,为了提高效率,我认为仅执行此操作一次,并将数据用于下拉列表中的所有不同选择。
问题是我该怎么做,如下所示:
我不能将其放入“page_load”事件中,因为每次页面加载时都会执行操作
我不能将其放入“dropdownlist_selectedindexchanged”事件中,因为那样它将与#1相同。
我想在“page_load”中执行此操作,如下所示
void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
Operation1();
Operation2();
Operation3();
}
}
这很好;该操作仅执行一次,我可以在整个过程中使用数据,但是由于操作需要时间,因此我的页面将需要一些时间来加载。
还有其他方法可以实现我想要的吗?请告诉我。
谢谢, 拉胡尔
In my code I have a dropdown selection and upon selection from dropdown the code performs further processing and generates report/data.
Further, the entire program depends on data which is gathered from 3 different operation
Operation1: processing a text files of size of size > 6MB
Operation2: SQL Query to a DB (Query takes around 1 minute)
Operation 3: HTTP POST request to server (The main costliest part of the programe)
So, to make it efficient I am thinking to perform this operation only once and use the data for all the different selection from dropdown.
Question is how can I do so as below:
I can't put it in "page_load" event because every time page loads the operations will carry out
I can't put it inside "dropdownlist_selectedindexchanged" event because then it will be same as #1.
I thought of doing it in "page_load" as below
void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{
Operation1();
Operation2();
Operation3();
}
}
This is fine; the operations gets performed only once and I can use the data throughout, but then my page will take time to load as the operations takes time.
Is there any other way I can achieve what I want? Please let me know.
Thanks,
Rahul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果数据集不会更改,您可能可以在
Application_Start()
处执行一次。编辑-类似这样的东西(从内存中输入并远离VS,我使用VB):
If the data set will not change, you probably could manage to do it once at
Application_Start()
.Edit - something like this (typing from memory and away from VS, i do VB):
缓存它。使用
CacheHelper
类从这里< /a>,你可以这样做:Cache it. Using the
CacheHelper
class from here, you could do: