Load DataSet within new thread??

  • Thread starter Thread starter Jonah Olsson
  • Start date Start date
J

Jonah Olsson

Hi,

I use the following code to load a dataset to be used to send out emails.
All this is executed as a new thread. The reason I implanted the
_currentPageNumber is to control the amount of emails sent to the SMTP
server in one load.

As I wrote in my post earlier tonight I'm very new to threading and the
following code works fine when not run in a separate thread.

Imports System
Imports System.net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports System.Threading

Public Class NewsletterMailer

Private Class SendWorker
Public NewsletterGUID As String
Public PageSize As Integer
Private _currentPageNumber As Integer
Private _totalRecords As Integer
Private SendMail As New EmailMessage
Private SMTP As New SMTP
Private _filedata As New FileData
Private _newsletterData As New NewsletterData

Public Sub Send()
Try
Dim NewsletterManagement As New NewsletterDB
Dim EmailGroupsManagement As New EmailGroupsDB
Dim CustomerManagement As New CustomerDB
Dim FilesManagement As New FilesDB
Dim MemberManagement As New MemberDB

_newsletterData =
NewsletterManagement.GetNewsletter(NewsletterGUID)
_currentPageNumber = 1

' ## Loop until all pages has been processed
While _currentPageNumber > 0

' Get emails
Dim dsMembers As DataSet =
MemberManagement.GetEmailsByGroup(_currentPageNumber, PageSize)

_totalRecords = dsMembers.Tables(1).Rows(0)(0)
If _totalRecords > 0 Then

' Prepare bulk send
' Do stuff here...

Dim dr As DataRow
For Each dr In dsMembers.Tables(0).Rows
' Code to send personalized email
SMTP.Send(SendMail)
SendMail.Recipients.Clear()
Next

Dim _totalPages As Integer
_totalPages =
CInt(System.Math.Ceiling((_totalRecords \ PageSize) + 1))

' Wait for system to catch up
Thread.Sleep(3000)

' Get next page number or 0 to end
If _currentPageNumber >= _totalPages Then
_currentPageNumber = 0
Else
_currentPageNumber += 1
End If
Else
_currentPageNumber = 0
End If

End While ' ## End loop

Catch ex As ThreadAbortException
System.Diagnostics.Debug.WriteLine("thread aborted: " &
ex.Message)
Finally
System.Diagnostics.Debug.WriteLine("cleanup")
End Try

End Sub
End Class

Private mailerThread As System.Threading.Thread
Public NewsletterGUID As String

Public Sub Send()
Dim SW As New SendWorker
With SW
.NewsletterGUID = NewsletterGUID
.PageSize = 400
End With

mailerThread = New System.Threading.Thread(AddressOf SW.Send)
mailerThread.Name = "SendNewsletter"
mailerThread.Start()
End Sub

End Class

And here's the code to invoke it:

Dim NewsletterMailer As New NewsletterMailer
_newsletterData = NewsletterManagement.GetNewsletter(NewsletterGUID)
NewsletterMailer.NewsletterGUID = NewsletterGUID
NewsletterMailer.Send()


Thankful for any kind help
Regards,
Jonah
 
It seems like I forgot to mention what the problem was :-)

When doing debug in VB .NET I couldn't access the DataSet loaded by the
SendWorker class, why?
Although it seems to work as planned when runing it on the server.

Any opinions on if this is a good way or a bad way of doing it?

Thanks
Jonah
 
Hi Jonah,

Couldn't access it in what way?
In the program, in a watch window, in immediate mode?
dsMembers is Nothing?
dsMembers can't be examined?

Regards,
Fergus

ps I'm off to bed now. ZZzzzs for several hours. :-)
 
Hi!

Sorry, I meant in the Output window during break. I couldn't access the
DataSet the function were loading. But when running the project on the
staging server it works perfect, so I guess nothing is wrong really... :-)

Jonah
 
Back
Top