Hello,
I'm writing a newsletter application which uses backgroundthrea ding. I'm using Session variable to report on progresswhile it loops through a dataset. The 'Status.aspx' pagerefreshes every 5 seconds while outputing the Session variables.My problem is, once the page redirects to 'Status.aspx' its showthe that's it only gets half through the dataset. If I increaseThread. Sleep to 2000 goes all the way through. I don't get anyerror message, it's like the MailObj disappears once the pageredirects. Do I need to use something like Context.Items.A dd ?
Any ideas would be great:)
Gavin Lyons
private void Send_Click(obje ct sender,System.W eb.UI.ImageClic kEventArgs e)
{
if (Subject.Text!= "")
{
string SQLString = "SELECT * FROM Members";
i386.Newsletter .mailer MailObj = new mailer();
MailObj.SMTPhos t = "mail.smtp.com" ;
MailObj.From = From.SelectedVa lue;
MailObj.Body = Body();
MailObj.Session s = true;
MailObj.EmailAd dressField = "UserID";
MailObj.EmailNa meField = "<!--FirstName--> <!--LastName-->";
MailObj.DataSet = i386.DatabaseSy stem.GetDataSet (SQLString,"Web ConfigDatabaseC onnection");
MailObj.WebCont ext =(System.Web.Ht tpContext)Sessi on["WebContext "];
Thread thread = new Thread(new ThreadStart(Mai lObj.Send));
thread.Priority = ThreadPriority. Lowest;
thread.Start();
Thread.Sleep(50 0);
Response.Redire ct("status.aspx ", true);
}
else
Label_Message.T ext="Missing data!";
}
public class mailer
{
private string _SMTPhost="loca lhost";
private string _Body="";
private string _From="";
private string _FromName="";
private string _Subject="";
private string _DatabaseConnec tion;
private string _EmailAddressFi eld;
private string _EmailNameField ;
private System.Web.Http Context _WebContext;
private DataSet _DataSet;
private bool _Sessions=true;
public string SMTPhost
{
get {return _SMTPhost;}
set {_SMTPhost=valu e;}
}
public string EmailNameField
{
get {return _EmailNameField ;}
set {_EmailNameFiel d=value;}
}
public string EmailAddressFie ld
{
get {return _EmailAddressFi eld;}
set {_EmailAddressF ield=value;}
}
public string Body
{
get {return _Body;}
set {_Body=value;}
}
public string Subject
{
get {return _Subject;}
set {_Subject=value ;}
}
public string From
{
get {return _From;}
set {_From=value;}
}
public string FromName
{
get {return _FromName;}
set {_FromName=valu e;}
}
public bool Sessions
{
get {return _Sessions;}
set {_Sessions=valu e;}
}
public DataSet DataSet
{
set {_DataSet=value ;}
get {return _DataSet;}
}
public System.Web.Http Context WebContext
{
set {_WebContext=va lue;}
get {return _WebContext;}
}
private string ReplaceFields(D ataRow DR, string String)
{
string ReplStr = String;
foreach (DataColumn Column in DR.Table.Column s)
{
ReplStr = ReplStr.Replace ("<!--" + Column.ToString () + "-->",DR[Column.ToString ()].ToString());
}
return ReplStr;
}
public void Send()
{
try
{
#region Initialise Sessions
if (this.Sessions)
{
lock(WebContext .Session.SyncRo ot )//lock the session object
{
StringBuilder sb = new StringBuilder() ; // for reporting
WebContext.Sess ion["StatusLine "] = "Retrieving data tosend ..";
WebContext.Sess ion["EmailRepor t"] = sb;
WebContext.Sess ion["EmailCount er"] = "0";
WebContext.Sess ion["TotalEmailsToS end"] =0;
WebContext.Sess ion["Done"] = "false";
WebContext.Sess ion["TotalEmailsToS end"] =this.DataSet.T ables[0].Rows.Count.ToS tring();
}
}
else
{
WebContext.Resp onse.Write("Ret rieving data to send ..");
}
#endregion
// Newsletter Merging
int startAt = Environment.Tic kCount;
int EmailCounter = 0;
bool TestMode = true;
foreach (DataRow DR in this.DataSet.Ta bles[0].Rows)
{
#region loop sending
// SMTP stmpobj = new SMTP(this.SMTPh ost);
// EmailMessage emailmsg = new EmailMessage();
string BodyForEmail = ReplaceFields(D R, this.Body);
string ToForEmail = DR[this.EmailAddre ssField].ToString();
string ToNameForEmail = ReplaceFields(D R,this.EmailNam eField);
string SubjectForEmail = ReplaceFields(D R, this.Subject);
if (this.Sessions)
{
lock(WebContext .Session.SyncRo ot )
{
WebContext.Sess ion[ "StatusLine " ] = "Processing " +ToForEmail;
}
}
try
{
if (!TestMode)
{
//stmpobj.Send(em ailmsg)
}
else
{
WebContext.Resp onse.Write("Sen ding to "+ ToForEmail + "("+ ToNameForEmail + ") " + WebContext.Sess ion[ "StatusLine "].ToString() + "<br/>");
}
if (this.Sessions)
{ // Reporting
((StringBuilder )WebContext.Ses sion["EmailRepor t"]).Append(EmailC ounter+ "," + DR["UserID"].ToString() +
", " + DateTime.Now.To String() + ", Successful<BR>" );//Reporting
}
}
catch (Exception ErrorMsg)
{
if (this.Sessions)
{ // Reporting
((StringBuilder )WebContext.Ses sion["EmailRepor t"]).Append(EmailC ounter+ "," + DR["UserID"].ToString() +
", " + DateTime.Now.To String() + ", Failed:" +ErrorMsg.Messa ge + "<BR>" );
}
}
EmailCounter++;
if (this.Sessions)
{
lock(WebContext .Session.SyncRo ot)
WebContext.Sess ion[ "EmailCount er" ] = EmailCounter;
}
#endregion
}
#region Sessions Final Values
if (this.Sessions)
{
lock( WebContext.Sess ion.SyncRoot )
{
int ms = Environment.Tic kCount - startAt;
int seconds = ms/1000;
WebContext.Sess ion[ "SecondsForMail Merge" ] =seconds.ToStri ng();
WebContext.Sess ion["Done"] = true;
WebContext.Sess ion["StatusLine "] = "NewsletterComp leted!";
}
}
#endregion
// this.DataSet.Di spose();
}
catch (Exception ex)
{
#region Session Report with Error
if (this.Sessions)
{
lock(WebContext .Session.SyncRo ot )
{
WebContext.Sess ion["MailMergeExcep tion"] = ex;
}
}
#endregion
WebContext.Resp onse.Write("Mai ler Error:" + ex.Message);
}
}
}
--------------------------------
From: Gavin Lyons
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>f7YLYLMHKUi +IHe+JkwBWg==</Id>