Securing threads on static methods in C #

I have code that I have in a static class / method, but I wanted to check that it would be thread safe. From what I read, I think it should be good, but something in the back of my mind says that it may not be. At the stage of processing the web page data, an external web service is used to create order records, and this can be quite slow: maybe from 30-40 seconds to maybe 5 or 10 minutes (this is from my hands), so I was going to shoot return the page back to the user, then start a new thread, and then send a message to the user after processing is complete. This is currently a static class / method. Provided that all my objects are created within the framework of a particular method (except for the standard default values ​​that will be distributed), this method should be thread safe,is not it. So, for example, if I had

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 breturnMessage);
            .... do something with return message
        }
        return check;
    }

    private static XmlDocument GetOrderXML(Order order)
    {
        ... creates an XML object for the order
    }
}

(CreateOrderXML - URL- -) -, ( xmlCo.Create(....)) ? , , , , , , , , , ?

+5
2

, ; . .

- . .

+12

- () , . , (, , ), , . .

, , lock . , . , A B:

lock( latch_a )
{
   process(object_a) ;
   lock ( latch_b )
   {
     process(object_a,object_b) ;
   }
}

, :

lock( latch_b )
{
   process(object_b) ;
   lock ( latch_a )
   {
     process(object_a,object_b) ;
   }
}

- , , , .

: . # lock. ( ) , . - :

class Widget
{
   private static readonly object X = new object() ;

   public void Foo()
   {
     lock(X)
     {
       // Do work using shared resource
     }
     return ;
   }

}
+5

All Articles