简体   繁体   中英

Performing Lengthy Functions in ASP.NET 4.0

I am a long time programmer that has just started web development in the past month. I am using ASP.NET 4.0 and C# on IIS 7.0. Thus far, I have had no problems, and have implemented libraries such as JQuery.

I am slowly learning the way of the web in that I am getting used to page post backs and AJAX implementation. 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.

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. Each loop processes the ping and then writes the results to the 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 ..."

I have experimented with UpdatePanels, UpdateProgress, and various Triggers. 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? 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. 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.

To implement this, I ended up creating the ping function as a 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.

$.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) {
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM