protected string ApiXmlPath;
protected string CustomerName; protected void Page_Load(object sender, EventArgs e)
{
ApiXmlPath = "http://open.client.api.com/list/api/";//xml文档型的Url
CustomerName = "123456";
if (!IsPostBack)
CreateXmlCustomerSelf();
} public void CreateXmlCustomerSelf()
{
string XFileName = CustomerName + ".xml";
FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + XFileName));
if (file.Exists)
file.Delete();
XmlTextWriter xtr = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + XFileName), Encoding.Default);
xtr.Close();
System.Threading.Thread ct = new System.Threading.Thread(new System.Threading.ThreadStart(XmlSaveLocation));
ct.Start();
// XmlSaveLocation();
} private void XmlSaveLocation()
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml");//异常位置 未将对象引用设置到对象的实例。
}
//System.Threading.Thread ct = new System.Threading.Thread(new System.Threading.ThreadStart(XmlSaveLocation));
//ct.Start();
XmlSaveLocation();不用线程的时候 没有任何做错 程序也完成了!一是用线程 就出现 未将对象引用设置到对象的实例。的异常报错~由于 Xml文档的Url 加载到本地 比较慢~ 所以想用 线程处理一下~大侠们 帮忙看看 到底是什么问题? 为什么是用线程 就会出异常分不多~ 多谢各位了
protected string CustomerName; protected void Page_Load(object sender, EventArgs e)
{
ApiXmlPath = "http://open.client.api.com/list/api/";//xml文档型的Url
CustomerName = "123456";
if (!IsPostBack)
CreateXmlCustomerSelf();
} public void CreateXmlCustomerSelf()
{
string XFileName = CustomerName + ".xml";
FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + XFileName));
if (file.Exists)
file.Delete();
XmlTextWriter xtr = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + XFileName), Encoding.Default);
xtr.Close();
System.Threading.Thread ct = new System.Threading.Thread(new System.Threading.ThreadStart(XmlSaveLocation));
ct.Start();
// XmlSaveLocation();
} private void XmlSaveLocation()
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(HttpContext.Current.Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml");//异常位置 未将对象引用设置到对象的实例。
}
//System.Threading.Thread ct = new System.Threading.Thread(new System.Threading.ThreadStart(XmlSaveLocation));
//ct.Start();
XmlSaveLocation();不用线程的时候 没有任何做错 程序也完成了!一是用线程 就出现 未将对象引用设置到对象的实例。的异常报错~由于 Xml文档的Url 加载到本地 比较慢~ 所以想用 线程处理一下~大侠们 帮忙看看 到底是什么问题? 为什么是用线程 就会出异常分不多~ 多谢各位了
protected string CustomerName;设置为静态的试试或者别在aspx页面里新开线程
全局变量 设置成静态的 也不好使~或者别在aspx页面里新开线程 这个怎么弄?没看懂
如果有dispose
请用
xtr.dispose()
试试
解决了 谢谢你哈把 private void XmlSaveLocation()
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml"));
}
改成
object sysobj = new object();
private void XmlSaveLocation()
{
lock (sysobj)
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml"));
}
}
就可以了~
解决了 谢谢你哈把 private void XmlSaveLocation()
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml"));
}
改成
object sysobj = new object();
private void XmlSaveLocation()
{
lock (sysobj)
{
XmlDocument x = new XmlDocument();
x.Load(ApiXmlPath);
x.Save(Server.MapPath("~/custom/XmlCache/" + CustomerName + ".xml"));
}
}
就可以了~