我有一些我目前在静态类/方法中的代码,但我想检查它是否是线程安全的.从我读过的内容来看,我认为这应该没问题,但我脑海中的一些事情却说它可能不是.我的网页的数据处理阶段使用外部Web服务来创建订单记录,这可能非常慢:可能是30-40秒,可能是5或10分钟(这不在我的手中)所以我要开火返回页面返回给用户,然后启动一个新线程,然后在处理完成后通过电子邮件发送给用户.这是一个静态类/方法.如果我的所有对象都是在特定方法中创建的(除了系统默认值,这是常见的)该方法应该是线程安全的,不应该.所以,例如,如果我有
public static class ProcessOrder()
{
public static int GetOrderMaxSize()
{
return (....gets and parses ConfigurationManager.AppSettings["MaxOrderSize"]...);
}
public static bool CreateOrder(Order order)
{
XmlDocument xmlDoc = GetOrderXML(order);
bool check = false;
using (CreateOrderXML.Create xmlCo = new CreateOrderXML.Create())
{
xmlCo.Timeout = 60000;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
string xmlString = "";
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlDoc.WriteTo(xmlWriter);
xmlWriter.Flush();
xmlString = stringWriter.GetStringBuilder().ToString();
}
}
byte[] bXMLOrder = encoding.GetBytes(xmlString);
byte[] breturnMessage;
check = xmlCo.Create(bXMLOrder, out …Run Code Online (Sandbox Code Playgroud)