多线程写文件问题

wujun_dry 2010-08-23 04:01:40
代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace testThreadLock
{
public partial class Form1 : Form
{
int threadNum = 10;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= threadNum; i++)
{
Thread t = new Thread(new ThreadStart(run));
t.Name = i.ToString();
t.Start();
}

}

private void run()
{
string filePath = @"D:\PHOTO\info.txt";
StreamWriter sw = File.CreateText(filePath);
sw.WriteLine("test");
sw.Close();
}
}
}

我想让10个线程同时同时往里写,应该会肯定用到锁吧,但是不知道怎么锁啊。怎么实现呢?

如果加了锁,是不是会在这个info.txt里出现十个test呢,

如果给
string filePath = @"D:\PHOTO\info.txt";
StreamWriter sw = File.CreateText(filePath);
sw.WriteLine("test");
sw.Close();
加了锁,如果我在这个run方法里还有其他的操作,会不会有影响呢?

菜鸟请教问题。谢谢各位高手!
...全文
121 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wujun_dry 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wuyazhe 的回复:]
写文件,大家竞争写,不就乱了么,本就要互斥的。我只是当楼主想练习,做例子这样写,实际这样写就太烂的设计了。
[/Quote]

嗯!学习了!还是菜鸟,练习中……
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
写文件,大家竞争写,不就乱了么,本就要互斥的。我只是当楼主想练习,做例子这样写,实际这样写就太烂的设计了。
deyygywxf 2010-08-23
  • 打赏
  • 举报
回复
锁是用来锁住需要互斥的资源的,其他的彼此不产生争夺的无所谓。
stream可以公开作为类成员变量。
run中,lock
StreamWriter sw = File.CreateText(@"D:\PHOTO\info.txt");
private void run()
{
lock(sw)
{
sw.Open();
sw.WriteLine("test");
sw.Close();
}
}

不过用了锁好象不能10线程一起写了吧
gomoku 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wujun_dry 的回复:]
...
这个我不知道啊,我因为要传很多很多张图片,然后并没有打包传,一张张传了,我就多开了几个线程同时传,但同时我也要为每张图片作记录,所以才会这样。
...
[/Quote]
这个不需要加锁(文件对象不同)。
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
没注意看。你原来线程就做这点事,那当例子也有点怪怪的了。如果频繁操作文件可以这样。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace CSharpWin02
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int threadNum = 10;
for (int i = 1; i <= threadNum; i++)
{
Thread t = new Thread(new ThreadStart(run));
t.Name = i.ToString();
t.IsBackground = true;//如果到界面退出还没执行完成,则随界面一起退出。
t.Start();
}
}

//创建文件,当前文件目录下创建info.txt
StreamWriter sw = File.CreateText(@"info.txt");
private void run()
{
lock (sw)
{
sw.WriteLine(Thread.CurrentThread.Name);
sw.Flush();//写入文件
}
}
}
}

wujun_dry 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dancingbit 的回复:]
比如说,专门用一个List,需要添加记录的时候在List中添加,对List加锁代价要小一些,然后完了一次性把List的内容写到文件中。
[/Quote]

关键我不会加锁。惭愧了
dancingbit 2010-08-23
  • 打赏
  • 举报
回复
比如说,专门用一个List,需要添加记录的时候在List中添加,对List加锁代价要小一些,然后完了一次性把List的内容写到文件中。
wujun_dry 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dancingbit 的回复:]
记录可以先保存在内存中,然后一次性写入。
[/Quote]

这个额没有接触过啊
wujun_dry 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]
锁是用来锁住需要互斥的资源的,其他的彼此不产生争夺的无所谓。
stream可以公开作为类成员变量。
run中,lock

例如:

C# code

StreamWriter sw = File.CreateText(@"D:\PHOTO\info.txt");
private void run()
{
lock(sw)
{
sw.Open……
[/Quote]

sw没有open属性啊,而且每次开线程,都会去执行
StreamWriter sw = File.CreateText(@"D:\PHOTO\info.txt");
,报错“文件“D:\PHOTO\info.txt”正由另一进程使用,因此该进程无法访问该文件。”
dancingbit 2010-08-23
  • 打赏
  • 举报
回复
记录可以先保存在内存中,然后一次性写入。
wujun_dry 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gomoku 的回复:]
引用楼主 wujun_dry 的回复:
……
我想让10个线程同时同时往里写
……

比单个线程写还要慢。
[/Quote]
这个我不知道啊,我因为要传很多很多张图片,然后并没有打包传,一张张传了,我就多开了几个线程同时传,但同时我也要为每张图片作记录,所以才会这样。

还请多帮帮忙啊。
gomoku 2010-08-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 wujun_dry 的回复:]
……
我想让10个线程同时同时往里写
……
[/Quote]
比单个线程写还要慢。
兔子-顾问 2010-08-23
  • 打赏
  • 举报
回复
锁是用来锁住需要互斥的资源的,其他的彼此不产生争夺的无所谓。
stream可以公开作为类成员变量。
run中,lock

例如:

StreamWriter sw = File.CreateText(@"D:\PHOTO\info.txt");
private void run()
{
lock(sw)
{
sw.Open();
sw.WriteLine("test");
sw.Close();
}
}
zilong4460072 2010-08-23
  • 打赏
  • 举报
回复
顶顶顶 我现在还在搜索怎么在C#中新开个线程呢

110,535

社区成员

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

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

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