'Start - not found' Threading library error

richt

Member
Licensed User
Hi agraham and forum bods,

I'm attempting to use your threading library to handle a batch process of sending e-mails but have a couple of issues.

1. I don't seem to gain any speed benefit from using a separate thread (even when compiled) as the main process still freezes until the e-mail process has completed.

2. Once the thread has finished the program crashes and presents a 'Start - not found' error.

I've put most of the offending code below, would be very grateful if anyone has any suggestions.

This code fills up an array for the thread to use and then hands over the e-mail process.

B4X:
'E-mail staff and ask if anyone is available
      
'Get all the e-mail addresses from the database
Main.Connection.BeginTransaction
Main.Command.CommandText = "SELECT StaffEmailAddress, StaffID FROM t_Staff WHERE StaffNotify = 'Yes';"
Main.Reader.Value = Main.Command.ExecuteReader
      
mcount = 0
      
'Assign data
Do While Main.Reader.ReadNextRow = True
   StaffAddress(mcount) = main.reader.GetValue(0)
   StaffID(mcount) = main.reader.GetValue(1)               
   mcount = mcount + 1               
Loop
      
Main.Reader.Close
' End transaction
Main.Connection.EndTransaction
            
'Pass the e-mailing task to a seperate thread.
rotamailthread.Start(RotaEmailSend(mcount,bookdate,bookstart,bookend))

This is the e-mailer thread;

B4X:
Sub RotaEmailSend(mcount,bookdate,bookstart,bookend)

AddObject("Mailer","SMTP")
AddObject("Message","DesktopMailMessage")

For p = 0 To mcount - 1
               
   stafadd = staffaddress(p)
   stafid = staffid(p)
            
   'Send an e-mail telling staff member about a booking
   Mailer.New1(Main.Settings.SMTPServer,25,Main.Settings.SMTPUsername,Main.Settings.SMTPPassword)
   Mailer.UseSSL = Main.Settings.SMTPUseSSL
   Message.New1
   Message.Subject = Main.OptSMTPOrg.Text & ": Cover required for booking."
   Message.From = Main.Settings.SMTPUsername
   Message.AddTo(stafAdd)
   MsgBody = "*** THIS IS AN AUTOMATED E-MAIL, PLEASE DO NOT REPLY ***<br><br>"
   MsgBody = MsgBody & "Cover is required for the following date:<br><br>"
   DateFormat("yyyy-mm-dd")
   Emaildate = DateParse(bookdate)
   DateFormat("dd/mm/yyyy")
   MsgBody = MsgBody & "Date: " & Date(Emaildate) & "<br>"
   DateFormat("yyyy-mm-dd")
   MsgBody = MsgBody & "Start: " & Bookstart & "<br>"
   MsgBody = MsgBody & "End: " & Bookend & "<br><br>" 
   MsgBody = MsgBody & "If you are able to cover please click the link below:<br><br>"
   MsgBody = MsgBody & "<a href='http://" & main.CompleteBookingDir.Text & "new/rota.php?staffmemberid=" & stafid & "&date=" & bookdate & "&starttime=" & bookstart & "&endtime=" & bookend & "'>Work this shift</a>"
               
   main.htmlemail.Value = Message.Value
    
   main.htmlemail.SetProperty("IsBodyHtml",True)
   
   Message.Body = MsgBody

   Mailer.Send(Message.Value)
   
Next

Mailer.Dispose
Message.Dispose

End Sub
 

agraham

Expert
Licensed User
Longtime User
B4X:
rotamailthread.Start(RotaEmailSend(mcount,bookdate,bookstart,bookend))
This line is wrong, you are calling RotaEmailSend directly and not invoking it on a thread. Thread.Start wants the name of the Sub to run, for arguments you will need to use global variables. Check the demo in the archive.
 

richt

Member
Licensed User
Ahh! Thank you agraham for your speedy reply. That's sorted it.

If I'd actually paid more attention to the documentation...Must RTFM.

Thanks again
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…