title: c# execute 2 threads simultaneously
body: <p>I am trying to reproduce a threading error condition within an HTTP Handler. </p>
  
  <p>Basically, the ASP.net worker procecss is creating 2 threads which invoke the HTTP handler in my application simultaneously when a certain page loads. </p>
  
  <p>Inside the http handler, is a resource which is not thread safe. Hence, when the 2 threads try to access it simultaneously an exception occurs. </p>
  
  <p>I could potentially, put a lock statement around the resource, however I want to make sure that it is infact the case. So I wanted to create the situation in a console application first.</p>
  
  <p>But i cant get 2 threads to execute a method at the same time like asp.net wp does. So, my question is how can you can create 2 threads which can execute a method at the same time.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The underlying resource is a sql database with a user table (has a name column only). Here is a sample code i tried.</p>
  
  <pre><code>[TestClass]
      public class UnitTest1
      {
          [TestMethod]
          public void Linq2SqlThreadSafetyTest()
          {
              var threadOne = new Thread(new ParameterizedThreadStart(InsertData));
              var threadTwo = new Thread(new ParameterizedThreadStart(InsertData));
  
              threadOne.Start(1); // Was trying to sync them via this parameter.
              threadTwo.Start(0);
  
              threadOne.Join();
              threadTwo.Join();
          }
  
  
          private static void InsertData( object milliseconds )
          {
              // Linq 2 sql data context
              var database = new DataClassesDataContext();
  
              // Database entity
              var newUser = new User {Name = "Test"};
  
              database.Users.InsertOnSubmit(newUser);
  
              Thread.Sleep( (int) milliseconds);
  
              try
              {
                  database.SubmitChanges(); // This statement throws exception in the HTTP Handler.
              }
  
              catch (Exception exception)
              {
                  Debug.WriteLine(exception.Message);
              }
          }
      }
  </code></pre>