负载测试异常 "file is being used by another process" C#
Load Tests Exception "file is being used by another process" C#
我正在使用 Visual Studio WebPerformance Tool,我的问题是我的所有 Web 测试都运行良好,但是当我 运行 负载测试(100 个用户)时出现异常"The process cannot access the file because it's being used by another process"。我的结论是并发用户试图同时读取同一个文件。
在负载下不工作的代码:
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();
这有效:
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();
我正在使用 Visual Studio WebPerformance Tool,我的问题是我的所有 Web 测试都运行良好,但是当我 运行 负载测试(100 个用户)时出现异常"The process cannot access the file because it's being used by another process"。我的结论是并发用户试图同时读取同一个文件。
在负载下不工作的代码:
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();
这有效:
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();