尝试同时进行文件操作

时间:2012-01-24 00:01:28

标签: c# powershell sql-server-2008-r2 asp.net-4.0 powershell-v2.0

我正在尝试创建一个执行以下操作的ASP.NET 4.0网站(C#):在用户单击按钮后,获取用户输入(从文本框)并在服务器上调用Powershell脚本。 Powershell脚本输出将记录到同一台计算机上的文本文件中。该脚本在SQL Server上执行一些存储过程。脚本执行完毕后,页面将读取日志文件的内容并在页面中返回。我记录这个的原因是因为我需要捕获这些操作的“详细”输出。

我遇到的问题是,如果另一个用户打开了网站并尝试进行相同操作,则在用户单击按钮后运行的整个命令集将排队。我不清楚为什么会这样。当在网站外部运行时,Powershell脚本并行执行。应用程序写入的日志文件使用时间戳和用户输入的信息,以确保它们不同,具体取决于输入信息的人。

以下是单击按钮时执行的代码:

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 + "' > " + 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;
    }

我希望找出是否有关于允许多个用户单击此按钮的信息,并且可以同时为每个用户运行后端脚本。我查看了异步页面和线程,但我不认为这在我的情况下是必要的,因为每个用户的页面应该有自己的线程。这里的任何帮助非常感谢。谢谢!

0 个答案:

没有答案
相关问题