关于File类Open方法中FileShare.ReadWrite的测试方法问题!

wokagoka 2009-11-13 10:58:08
我想测试的是当我的ASP.NET多个请求正在访问文件的时候,我有一个程序会在后台修改该文件,以下是两种测试方法。

以下代码在跑时会出现文件访问冲突!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace FileShareTest
{
class Program
{
static void Main(string[] args)
{
FileWriter fw = new FileWriter();
FileReader fr = new FileReader();
Thread thw = new Thread(new ParameterizedThreadStart(fw.Run));
Thread thr = new Thread(new ParameterizedThreadStart(fr.Run));
Flag.IsRun = true;
thw.Start();
thr.Start();
Console.ReadKey();
Flag.IsRun = false;
}
}

static class Flag
{
private static bool isRun;
public static bool IsRun
{
get { return isRun; }
set { isRun = value; }
}
}
class FileWriter
{
private int iRequest = 0;
public int GetRequest()
{
return iRequest;
}

public void Run(object obj)
{
while (Flag.IsRun)
{
//Random r = new Random(DateTime.Now.Second);
//try
//{

// using (FileStream fs = File.Open(@"D:\Work\Samples\FileShareTest\bin\1.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
// {
// try
// {
// using (StreamWriter sw = new StreamWriter(fs))
// {
// try
// {
// sw.Write(r.Next(1000000, 100000000));
// sw.Write(r.Next(1000000, 100000000));
// iRequest++;
// Console.WriteLine("Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************Writed**************");
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// sw.Close();
// }
// }
// }
// finally
// {
// fs.Close();
// }
// }
//}
//catch (IOException ex)
//{
// Console.WriteLine(string.Format("WritedCount {0}", iRequest));

// throw ex;
//}
//finally
//{

//}

//Thread.Sleep(100);

File.Copy(@"D:\Work\Samples\FileShareTest\bin\1.txt", @"D:\Work\Samples\FileShareTest\bin\Debug\test.txt", true);
}
}
}

class FileReader
{
private int iRequest = 0;
public int GetRequest()
{
return iRequest;
}

public void Run(object obj)
{
while (Flag.IsRun)
{

using (FileStream fs = File.Open(@"D:\Work\Samples\FileShareTest\bin\Debug\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
try
{
Console.WriteLine(string.Format("Read {0}", ++iRequest));
}
catch (System.IO.IOException ioEx)
{
Console.WriteLine("B:IOException.");
}
finally
{
fs.Close();
}
}
}
}
}
}

以下代码不会冲突!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace MulThreadReadAndWriteFile
{
class Program
{

private static List<FileStream> list;
static void Main(string[] args)
{
if (list == null)
{
list = new List<FileStream>();
}
string str1, str2;
for (int i = 0; i < 10000000; i++)
{
Thread t1 = new Thread(new ParameterizedThreadStart(OpenFile));
Thread t2 = new Thread(new ParameterizedThreadStart(WriteFile));
str1 = string.Format("{0}-1", i);
str2 = string.Format("{0}-2", i);
t1.Start(str1);
t2.Start(str2);

}

for (int i = 0; i < list.Count - 1; i++)
{
list[i].Close();

//Console.WriteLine("Closed file");
}

Console.ReadKey();
}

private static void OpenFile(object obj)
{
//d Console.WriteLine(obj);
FileStream stream = File.Open(@"D:\Work\Samples\FileShareTest\bin\Debug\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

try
{
list.Add(stream);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//Console.WriteLine("Open file");
}

}

private static void ReplaceFile()
{
File.Copy(@"D:\WindRes\ImagePool\Latest\Company\source.txt", @"D:\WindRes\ImagePool\Latest\Company\typecode\dest.txt", true);
Console.WriteLine("Replace file");
}

private static void WriteFile(object obj)
{
Console.WriteLine(obj);
FileStream stream = File.Open(@"D:\WindRes\ImagePool\Latest\Company\typecode\dest.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
Console.WriteLine(obj);
try
{

byte[] btData = new UTF8Encoding().GetBytes("I am dest. Writed file");

stream.Write(btData, 0, btData.Length);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//Console.WriteLine("Writed file");
stream.Close();
}
}
}
}

...全文
608 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wokagoka 2009-11-13
  • 打赏
  • 举报
回复
谁知道呀,人呢
wokagoka 2009-11-13
  • 打赏
  • 举报
回复
咔咔
wokagoka 2009-11-13
  • 打赏
  • 举报
回复
哎……
StillMiss 2009-11-13
  • 打赏
  • 举报
回复
我不知道,来学习

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧