HOW TO USE: BackgroundWorker

Hi,

Anybody here can help me please. If I execute the function below, and the records was all up to 20K, my program is not responding. The function is used to get the total rows within the date range on my criteria. I am using FireBird database. I overheard that using BackgroundWorker will do, but I really don't have an idea on how to use it.

Where do I use the BackgroundWorker in my code as follows:

Example:

PrivateFunction GetTotalRows()AsInteger

Dim dQueryAs DateTime

Dim dStartAs DateTime =Me.dtpFrom.Value.Date

Dim dEndAs DateTime =Me.dtpTo.Value.Date

Try

iCtr = 0

' Loop through database record

SQL("SELECT * FROM logrecord")

If Dr.HasRows =TrueThen

DoWhile Dr.Read

dQuery =CDate(Dr.Item("logdate").ToString)

If dQuery.Date >= dStart.DateAnd dQuery.Date <= dEnd.DateThen

iCtr = iCtr + 1

EndIf

Loop

EndIf

iRowCount = iCtr

' Clean

Dr.Close()

Cmd.Dispose()

Catch exAs SqlException

MsgBox(ex.ToString, MsgBoxStyle.Critical,"ODBC Error")

Catch exAs Exception

MsgBox(ex.ToString, MsgBoxStyle.Critical,"General Error")

EndTry

Return iRowCount

EndFunction

ThanksSad

[3277 byte] By [CyberJunkie] at [2008-1-10]
# 1

This is the basic code to use a backgroundworker

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Me.BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

your code here

End Sub

js06 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

A backgroundworker is designed to allow multithreading. It doesn't appear that you would benefit from this approach.

My guess is that your query execution is timing out due to a large resultset returned.

My suggested would be to incorporate your conditional logic into your query and only return the count of log records within your given range.

Using your posted logic, try this:

Private Function GetTotalRows() As Integer

Dim dStart As DateTime = Me.dtpFrom.Value.Date

Dim dEnd As DateTime = Me.dtpTo.Value.Date

Try

' Loop through database record

SQL("SELECT COUNT(logrecord) [myCount] FROM logrecord WHERE logrecord BETWEEN '" & dStart.Date & "' AND '" & dEnd.Date & "'")

If Dr.HasRows = True Then

While Dr.Read

iRowCount = CDate(Dr.Item("myCount").ToString)

End While

End If

' Clean

Dr.Close()

Cmd.Dispose()

Catch ex As SqlException

MsgBox(ex.ToString, MsgBoxStyle.Critical, "ODBC Error")

Catch ex As Exception

MsgBox(ex.ToString, MsgBoxStyle.Critical, "General Error")

End Try

Return iRowCount

End Function

AdamD.Turner at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...