title: Attempting Simultaneous File Operations
body: <p>I am attempting to create an ASP.NET 4.0 website (C#) that does the following: after the user clicks a button, take user inputs (from text boxes) and call a Powershell script on the server.  The Powershell script output is logged to a textfile on the same machine.  The script executes some stored procedures on a SQL server.  Once the script is done executing, the page reads the contents of the log file and returns them in the page.  The reason I log this as I do is because I need to capture the "verbose" output of these operations.</p>
  
  <p>The problem I am having is that the whole set of commands that are run after the user clicks the button is queued if another user has the site open and is attempting the same operation.  I am not clear as to why this happens.  When run outside of the website, the Powershell scripts execute in parallel.  The log files that the application writes to use a timestamp and user inputted information to ensure that they are different depending on who is inputting the info.</p>
  
  <p>Here is the code that is executed when the button is clicked:</p>
  
  <pre><code>protected void Test_Click(object sender, EventArgs e)
     {
          // Get the current date/time to use a timestamp for the log file
          DateTime curDate = DateTime.Now;
          string logFileName = "D:\\test1\\logs\\" + curDate.Year + curDate.Month + curDate.Day +
              curDate.Hour + curDate.Minute + curDate.Second + curDate.Millisecond + "_" +
              TextBox1.Text.Replace("\\", "_") + "_Test.txt";
  
          // Clean the Result TextBox
          ResultBox.Text = string.Empty;
  
          // Initialize PowerShell engine
          var shell = PowerShell.Create();
  
          // Add a script to the PowerShell shell object that will execute the instructions with the given input
          shell.Commands.AddScript("powershell.exe -file D:\\test1\\ps_script0.ps1 '" + TextBox3.Text + "' '" +
              TextBox1.Text + "' '" + TextBox2.Text + "' '" + TextBox4.Text + "' &gt; " + logFileName);
  
          // Execute the script
          shell.Invoke();
  
          // Create a StreamReader object to read the results of the script instructions and then store the file's contents in a string
          StreamReader sr = File.OpenText(logFileName);
          String results = sr.ReadToEnd();
          results = results.Replace("VERBOSE: ", "");
          sr.Close();
  
          // Set the text of the result box to the string that was read in
          ResultBox.Text = results;
      }
  </code></pre>
  
  <p>I am hoping to find out if there is information I can read about allowing multiple users to click this button, and have the backend script run simultaneously for each of them.  I looked into asynchronous pages and threads, but I don't think this is necessary in my case since each user's page should have its own thread.  Any help here is much appreciated.  Thanks!</p>