Solved

possible timeout issues when using my ui

Posted on 2008-10-01
3
228 Views
Last Modified: 2012-05-05
Hi,

We currently have a piece of sql that runs that can take a long time, over minutes wouldnt be unusual purely due to the amount of data that is being processed.

I have been asked to put a quick user interface together just so that marketing can kick this job off them selves.

So basically on a button click i will be calling a stored procedure that will run this sql.

What i am worried about is the amount og time it takes to run, will the code timeout etc?

what is the best way to handle this kind of thing? A thread or webservice or?.....

This is the first time I have had to do something that comes up against this issue so dont know what the 'norm' is for handling.

any advice would be appreacited on best practice or at how it would normally be handled.

I would need to some how let the user know that the job has finished via the ui.
0
Comment
Question by:scm0sml
  • 2
3 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 22612526
Hi,

First of all, will the UI be a winform or a webform? Secondly, regardless of question one, putting it on a separate worker thread will separate it from the UI.

/Carl.
0
 

Author Comment

by:scm0sml
ID: 22612540
It will be a webform.

Will the worker thread be able to report back to the ui to display some notification that it is completed?
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 22613987
Hi,

Yes, please consider the attached code snippet for reference on how to set it up.

/Carl.
WorkerThreadPage.aspx:
 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="WorkerThreadPage.aspx.vb" Inherits="WorkerThreadPage" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Background Worker Thread Test</title>

</head>

<body>

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>

    <div style="border: Solid 1px Gray;padding: 5px 5px 5px 5px;margin-top: 50px;">

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">

            <ContentTemplate>

                <div id="processDiv" runat="server">

                </div>           

            </ContentTemplate>

            <Triggers>

                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

            </Triggers>

        </asp:UpdatePanel>

        <asp:Button ID="btnStartProcess" Text="Start Background Process" runat="server" />

        <asp:Timer ID="Timer1" runat="server" Interval="1000">

        </asp:Timer>

    </div>

    </form>

</body>

</html>
 

WorkerThreadPage.aspx.vb:
 

Imports System.Threading
 

Partial Class WorkerThreadPage

    Inherits System.Web.UI.Page
 

    Private Event ThreadCompleted()

    Private Event ThreadProgress(ByVal threadProgress As Double)
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

            Dim wThread As Thread = New Thread(AddressOf DoBackgroundWork)

            wThread.IsBackground = True

            Session("WorkerThread") = wThread

            AddHandler ThreadCompleted, AddressOf WorkerThreadCompleted

            AddHandler ThreadProgress, AddressOf WorkerThreadProgress

            Timer1.Enabled = False

            Session("IsComplete") = False

            Session("MaxRunningTime") = 100

            Session("ThreadProgress") = 0

        End If

        If Page.IsPostBack Then

            If (CType(Session("WorkerThread"), Thread).IsAlive = True) And (CType(Session("IsCompleted"), Boolean) = True) Then

                CType(Session("WorkerThread"), Thread).Abort()

            End If

        End If

    End Sub
 

    Protected Sub btnStartProcess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartProcess.Click

        CType(Session("WorkerThread"), Thread).Start()

        processDiv.InnerText = "Processing Background Worker Thread, Status: 0%"

        Timer1.Enabled = True

    End Sub
 

    Private Sub DoBackgroundWork()

        Dim i As Integer = 0

        Do While i < CInt(Session("MaxRunningTime")) + 1

            If (i = CInt(Session("MaxRunningTime"))) Then

                RaiseEvent ThreadCompleted()

            End If

            RaiseEvent ThreadProgress(CDbl(100 * (i / CInt(Session("MaxRunningTime")))))

            Thread.Sleep(1000)

            i += 1

        Loop

        i = 0

    End Sub
 

    Private Sub WorkerThreadCompleted()

        Session("IsCompleted") = True

    End Sub
 

    Private Sub WorkerThreadProgress(ByVal threadProgress As Double)

        If threadProgress.ToString.Length > 5 Then

            Session("ThreadProgress") = threadProgress.ToString.Substring(0, 5)

        Else

            Session("ThreadProgress") = threadProgress.ToString

        End If

    End Sub
 

    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        processDiv.InnerText = "Processing Background Worker Thread, Status:" & Session("ThreadProgress").ToString & "%"

        If Session("IsCompleted") Then

            processDiv.InnerText = "Processing Background Worker Thread, Status: Finished!"

            Timer1.Enabled = False

        End If

    End Sub
 

End Class

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Web API or ASP.NET Core MVC? 3 51
Put window Form inside tab page 10 26
Message not shown 5 27
Exit the loop 4 31
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now