操作多线程向一个文件写数据

Firestone2003 2007-06-25 02:58:36
try
{
int ThreadIndex = Convert.ToInt32(threadIndex);
sw.Start();
FileStream fs = new FileStream(this.filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
FileStream nfs = new FileStream(this.NewFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryWriter bw = new BinaryWriter(nfs);

int ReadedLength;
byte[] buffer = new byte[EachFileSize[ThreadIndex]];
fs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);
ReadedLength = br.Read(buffer, 0, EachFileSize[ThreadIndex]);
nfs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);
bw.Write(buffer, 0, ReadedLength);

sw.Stop();
fs.Close();
nfs.Close();
br.Close();
bw.Close();
MessageBox.Show(ThreadIndex.ToString() + "done");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

private int[] FilePosition;//每个线程开始位置;
private int[] EachFileSize;//每个线程应读取的大小;

问题:
程序没有异常,但是文件头部分都是空值,EachFileSize倍数(随机)的部分总是空值,不知道是什么原因?
Mutex也没有用
但设成1个线程没有问题。
...全文
439 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hq2008 2007-07-24
  • 打赏
  • 举报
回复
v
Firestone2003 2007-06-25
  • 打赏
  • 举报
回复
FileStream nfs = new FileStream(this.NewFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);


问题解决,谢谢大家捧场!
一个不慎啊
Firestone2003 2007-06-25
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filePath = this.openFileDialog1.FileName;
NewFilePath = this.saveFileDialog1.FileName;
int threadCount = Convert.ToInt32(this.numericUpDown1.Value);
int eachFileSize = (int)(File.OpenRead(filePath).Length) / threadCount;
int LastFileSize = eachFileSize + (int)(File.OpenRead(filePath).Length) % threadCount;
EachFileSize = new int[threadCount];
FilePosition = new int[threadCount];
for (int m = 0; m < threadCount - 1; m++)
{
EachFileSize[m] = eachFileSize;
}
EachFileSize[threadCount - 1] = LastFileSize;
for (int n = 0; n < threadCount; n++)
{
FilePosition[n] = n * eachFileSize;
}


Thread[] thrArray = new Thread[threadCount];
for (int j = 0; j < threadCount; j++)
{
thrArray[j] = new Thread(new ParameterizedThreadStart(MutilThreadCopy));
thrArray[j].Start(j);
}

}
}
}


private void MutilThreadCopy(object threadIndex)
{
try
{
int ThreadIndex = Convert.ToInt32(threadIndex);
sw.Start();
FileStream fs = new FileStream(this.filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
FileStream nfs = new FileStream(this.NewFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryWriter bw = new BinaryWriter(nfs);

int ReadedLength;
byte[] buffer = new byte[EachFileSize[ThreadIndex]];
fs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);
ReadedLength = br.Read(buffer, 0, EachFileSize[ThreadIndex]);
nfs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);
bw.Write(buffer, 0, ReadedLength);
bw.Flush();
sw.Stop();
fs.Close();
nfs.Close();
br.Close();
bw.Close();
MessageBox.Show(ThreadIndex.ToString() + "done");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

做了一个具体测试,每次都是0号线程数据写不进去,在bw.Write(buffer, 0, ReadedLength);前面加了一行bw.Write(buffer, 0, 0);以后,5个以下线程没有问题,线程数超过5个,0号线程的数据又写不进去。每次文件头到EachFileSize的地方都是空值
wzd24 2007-06-25
  • 打赏
  • 举报
回复
楼主写的是什么???
没看见哪里用了多线程!!!
RedGoldFish 2007-06-25
  • 打赏
  • 举报
回复
我假设你各个线程读写的都是不同的文件,否则这里别忘了做同步.
greatqn 2007-06-25
  • 打赏
  • 举报
回复
文件读可以多线程,写只能单线程吧。
CathySun118 2007-06-25
  • 打赏
  • 举报
回复
要作线程同步
RedGoldFish 2007-06-25
  • 打赏
  • 举报
回复
你的程序是否保证了对下边两个Class Level 的变量付值是在每个线程使用它之前? 或者说对它们的读写是否保证了Thread Safe.

private int[] FilePosition;//每个线程开始位置;
private int[] EachFileSize;//每个线程应读取的大小;
Ivony 2007-06-25
  • 打赏
  • 举报
回复
没看到你哪里有多线程

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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