我需要在后台线程中呈现控件并通过邮件发送输出。所以我让这段代码在后台线程中运行:
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(sendmail), new Object());
public void sendmail(object o)
{
UserControl cntrl = new Page().LoadControl("~/mycontrol.ascx") as UserControl;
StringWriter stringwriter = new StringWriter();
Html32TextWriter htmlwriter = new Html32TextWriter(stringwriter);
cntrl.RenderControl(htmlwriter);
SendEmailUsingNetMail(stringwriter);
}
At runtime I am getting this exception
System.NullReferenceException: Object reference NOT SET TO an instance of an object.
AT System.Web.UI.HtmlControls.HtmlForm.GetActionAttribute()
AT System.Web.UI.HtmlControls.HtmlForm.RenderAttributes(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlControl.RenderBeginTag(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
AT System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output)
AT System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer)
AT System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
AT System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
AT System.Web.UI.Control.Render(HtmlTextWriter writer)
AT System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
AT System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
AT RedBus.RBMail.sendmail(object o)
从 asp.net 请求线程运行时,相同的代码运行良好。是否有可能让这个东西在后台线程本身工作?如果是怎么办?
谢谢,普拉丹