简体   繁体   English

负载测试异常“文件正在被另一个进程使用” C#

[英]Load Tests Exception “file is being used by another process” C#

I'm using Visual Studio WebPerformance Tool and my problem is that all of my web-tests are working perfectly but when I run Load test (100 users) I'm getting an exception "The process cannot access the file because it's being used by another process". 我使用的是Visual Studio WebPerformance工具,我的问题是我所有的网络测试都能正常运行,但是当我运行负载测试(100个用户)时,我遇到了一个异常“该进程无法访问该文件,因为该文件正在被该文件使用另一个过程”。 My conclusion is that concurrent users are trying to read the same file at the same time. 我的结论是,并发用户正在尝试同时读取同一文件。

Code that did not work under the load: 在负载下不起作用的代码:

int x; //user number
if(File.Exists("C:\\Users\\Desktop\\Tests\\users5a.txt"))
{
    var last_number = File.ReadAllLines("C:\\Users\\Desktop\\Tests\\users5a.txt").Last();
    x = int.Parse(last_number) + 1;
}
else
    x = 0;

System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Desktop\\Tests\\users5a.txt", true);
file.WriteLine(x);
file.Close();

This worked: 这工作:

 EventWaitHandle waitHandle = new EventWaitHandle(true,EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");

    waitHandle.WaitOne();

    int x; //user number
    if (File.Exists("C:\\Users\\Desktop\\Tests\\users5a.txt"))
    {
        var last_number = File.ReadAllLines("C:\\Users\\Desktop\\Tests\\users5a.txt").Last();
        x = int.Parse(last_number) + 1;
    }
    else
        x = 0;

    System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Desktop\\Tests\\users5a.txt", true);
    file.WriteLine(x);
    file.Close();
    file.Dispose();
    waitHandle.Set();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM