Search Engine Optimization And Why You Gotta Use It

E-commerce is a cut throat business. You have to arm yourself with the proper know-how and the tools to make your site a cut above the rest. Each day, more and more sites are clambering to optimize their rankings in websites and if you lose your guard, you may just get trampled on and be left in the abyss filled with so many failed e-commerce sites. Continue reading “Search Engine Optimization And Why You Gotta Use It”

Advertisements

How to execute long running queries in the background using vb.net

Data-ProcessingSometimes the users to your website will have to wait for an operation to be completed. Sometimes, the wait time is minor – a few seconds at most, sometimes though, this can take a lot longer – spanning from 5-10 minutes to a few hours.

You can optimize your queries to be as efficient as you would like them, but when dealing with a lot of data, this time cannot be reduced significantly.

We have written a few functions to help you with the process.

First, you will need to create a new class that will handle all long running processes:

Imports Microsoft.VisualBasic

Public Class LongProcesses

    Public Sub MatchAllDocuments(params As Object)
        Dim params_local() As Object = CType(params, Object())
        Dim pr As New ProcessFile
        Dim d As New DataLayer
        Dim dtStart As DateTime = Now
        Dim clName As String = d.GetClientName(params_local(0).ToString)
        pr.WriteToLog("Matching all Documents for client " + clName, params_local(0).ToString, params_local(1).ToString)
        Dim strerror As String = d.MatchAllDocuments(Integer.Parse(params_local(0).ToString), params_local(1).ToString)

        Dim ts As TimeSpan = Now.Subtract(dtStart)
        If strerror = "" Then
            Dim strMsg As String = "Dear Administrator,<br /><br />Matching has been completed for client <strong>" + clName
            strMsg += "</strong>.<br />Time required to complete this operation: " + ts.Minutes.ToString() + "min " + ts.Seconds.ToString + "s"
            strMsg += "<br /><br />Thank you,<br />Web Team"
            SendEmailC.SendEmail("support@carra-lucia-ltd.co.uk", "Matching Complete for Client " + clName, strMsg)
        Else
            Dim strMsg As String = "Dear Administrator,<br /><br />Matching could not be completed for client <strong>" + clName
            strMsg += "</strong>.<br />Time required to complete this operation: " + ts.Minutes.ToString() + "min " + ts.Seconds.ToString + "s"
            strMsg += "<br />Error: " + strerror
            strMsg += "<br /><br />Thank you,<br />Web Team"
            SendEmailC.SendEmail("support@carra-lucia-ltd.co.uk", "Matching Complete for Client " + clName, strMsg)
        End If
        pr.WriteToLog("Matching Finished for client " + clName, params_local(0).ToString, params_local(1).ToString)
    End Sub
End Class

You can see above that the MatchAllDocuments method accepts a single parameter of type Object (this is required for the ParameterizedThreadStart method) and that in this case that parameter will be an Object[] array containing more than one value. I prefer to email the results when completed to the user. Alternatively, you can decide to write to a file or use a session variable to store the results.

In my Global.asax.vb class, I have the following method:

<%@ Import Namespace="System.Threading" %>
 Public Shared Sub MatchClient(params() As Object)
        Dim pc As New LongProcesses
        Dim ts As New ParameterizedThreadStart(AddressOf pc.MatchAllDocuments)
        Dim thd As Thread
        thd = New Thread(ts)
        thd.IsBackground = True
        thd.Start(params)
    End Sub

Because the method is Shared, it can be accessed from anywhere in my code.
A new instance of the LongProcesses class is created on every call to the static method. Note also that the thread we create is set to be a background thread. This method call will not block – it will return almost immediately because it is not being called on the main thread.

On the main button click we have the following event:

Protected Sub btnMatchAll_Click(sender As Object, e As EventArgs)
        Dim parms() As Object = New Object(1) {cboClients.SelectedValue, Session("Username")}
        ASP.global_asax.MatchClient(parms)

        lblMessage.Text = "Matching process has been started and you will receive an email when this process is complete."
        lblMessage.Visible = True

    End Sub

The result of the button push is an immediate return to screen with a message and also an email within the next few minutes like the one below:

Email result. Confidential information has been blurred out.
Email result. Confidential information has been blurred out.