• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

棒!读写文件流加锁示例,但是不知道怎么管时序


Recommended Posts

` private static ReaderWriterLockSlim _ rwl=new ReaderWriterLockSlim(lockructionpolicy .支持撤销);//暂时不知道和下面的有啥区别

私有void Form1_Load(对象发送方,EventArgs e)

{

线程线程1=新线程(()=

{

而(真)

{

SaveCSV('a ',' e : \ \ project。CSV’);

}

});

线程线程2=新线程(()=

{

而(真)

{

SaveCSV('b ',' e : \ \ project。CSV’);

}

});

线程1。start();

线程2。start();

}

静态ReaderWriterLockSlim LogWriteLock=new ReaderWriterLockSlim();

公共void SaveCSV(字符串列,字符串文件名)

{

尝试

{

日志写锁.输入写锁();//加锁

如果(!文件。存在(文件名))

{

使用(FileStream fs=新文件流(文件名,系统.IO。FileMode。新建))

{

使用(StreamWriter SW=new StreamWriter(fs,System .文字。编码。默认))

{

西南WriteLine('NG模组码,托盘码,NG代码');//写入硬盘

西南换行=。环境。换行;

}

}

}

其他

{

使用(FileStream fs=新文件流(文件名,系统.IO。FileMode.Append))

{

使用(StreamWriter SW=new StreamWriter(fs,System .文字。编码。默认))

{

西南WriteLine(列);//写入硬盘

西南换行=。环境。换行;

}

}

}

}

接住(例外ex)

{

}

最后

{//解锁

日志写锁.ExitWriteLock();

}

}`

Link to comment
Share on other sites