[英]Performing Lengthy Functions in ASP.NET 4.0
I am a long time programmer that has just started web development in the past month. 我是一个长期的程序员,在过去的一个月中刚刚开始进行Web开发。 I am using ASP.NET 4.0 and C# on IIS 7.0.
我在IIS 7.0上使用ASP.NET 4.0和C#。 Thus far, I have had no problems, and have implemented libraries such as JQuery.
到目前为止,我还没有遇到任何问题,并且已经实现了JQuery之类的库。
I am slowly learning the way of the web in that I am getting used to page post backs and AJAX implementation. 我正在慢慢学习网络的方式,因为我习惯了页面回发和AJAX实现。 I recently attempted to add functionality to my website which I have not been able to implement, even with the help of Stack Overflow and Google.
我最近尝试在我的网站上添加一些我无法实现的功能,即使借助Stack Overflow和Google也是如此。
Allow me to frame the problem: 请允许我描述问题:
I am attempting to create a function on a web form which allows me to click a button and then have the server query a DB for a list of hostnames and ping each of the names in the result. 我正在尝试在Web表单上创建一个函数,该函数使我可以单击按钮,然后让服务器查询数据库以获取主机名列表,并ping结果中的每个名称。 Each loop processes the ping and then writes the results to the DB.
每个循环处理ping,然后将结果写入DB。 Along the way, though not entirely needed, I am interested in retrieving progress from the loop so as to update the page with information like "Pinging SomeHostname ..."
一路上,尽管不是完全需要,但我有兴趣从循环中检索进度,以便使用“ Ping SomeHostname ...”之类的信息更新页面。
I have experimented with UpdatePanels, UpdateProgress, and various Triggers. 我已经尝试了UpdatePanels,UpdateProgress和各种触发器。 In all of my experiences, the page either locks up or I receive a timeout during debug.
从我的所有经验来看,页面要么被锁定,要么在调试期间收到超时。
My last (non-working) solution looked like this: 我的最后一个(无效)解决方案如下所示:
The web form: 网络表单:
<asp:UpdatePanel ID="PingUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btnPingAll" runat="server" Text="Ping All" OnClick="PingAll_Click"></asp:Button>
<asp:Timer ID="pingUpdateTimer" OnTick="pingUpdateTimer_Tick" Interval="1000" runat="server" />
<div id="pingStatus" class="pingStatusBox" runat="server">
<asp:Label ID="hostName" runat="server" Text="Initializing ping..."></asp:Label>
</div>
</ContentTemplate>
-- TRIGGERS HERE --
</asp:UpdatePanel>
The code behind: 后面的代码:
protected void PingAll_Click(object sender, EventArgs e)
{
Thread pingThread = new Thread(new ThreadStart(PingHosts));
pingThread.IsBackground = true;
pingThread.Start();
}
private void PingHosts()
{
//Get information DataTable
if (dataTable != null)
{
foreach (DataRow row in dataTable.Rows)
{
// Set label
hostName.Text = row[0].ToString();
PingUpdatePanel.Update();
// Ping stuff and update DB
}
}
}
Could anyone point me toward how to correctly approach this in web programming/ASP.NET? 谁能指出我如何在Web编程/ASP.NET中正确地实现这一目标? This information can be used later on to say generate lengthy reports.
以后可以使用此信息来生成冗长的报告。
Let me know if I can be more clear, I appreciate any help! 让我知道是否可以说得更清楚,非常感谢您的帮助!
The problem I think you are having is that you are executing code on a different thread than the ui thread. 我认为您遇到的问题是,您正在与ui线程不同的线程上执行代码。 so you either need cross thread communication ( http://www.dreamincode.net/forums/topic/35616-cross-thread-communication-in-c%23/ ) or update a data table and read using the timer as per my comment above.
因此,您需要跨线程通信( http://www.dreamincode.net/forums/topic/35616-cross-thread-communication-in-c%23/ )或更新数据表并根据我的要求使用计时器进行读取在上面发表评论。
To implement this, I ended up creating the ping function as a WebMethod. 为了实现这一点,我最终将ping函数创建为WebMethod。 From there, I pulled the IPs on pageload (as suggested by Jon P), stored them in a hidden field, and used JQuery AJAX in a loop to perform the function.
从那里,我将IP加载到页面加载中(如Jon P的建议),将它们存储在一个隐藏字段中,并在循环中使用JQuery AJAX执行该功能。
$.ajax({
type: "POST",
url: "PingIP.aspx/Ping",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ "ipAddress": ipAddress }),
processData: false,
success: function (msg) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.