StreamWriter多线程C# [英] StreamWriter Multi Threading C#

查看:76
本文介绍了StreamWriter多线程C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的代码寻求帮助.我是新手,想通过写入文本文件来实现安全的多线程.

I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file.

    StreamWriter sw = new StreamWriter(@"C:\DailyLog.txt");
    private void Update(){
        var collection = Database.GetCollection<Entity>("products");
        StreamReader sr = new StreamReader(@"C:\LUSTK.txt");
            string[] line = sr.ReadLine().Split(new char[] { ';' });
            while (!sr.EndOfStream)
            {
                    line = sr.ReadLine().Split(new char[] { ';' });
                    t = delegate { 
                           UpdateEach(Convert.ToInt32(line[5])); 
                        }; 
                     new Thread(t).Start();
            }
            sr.Close();

    }
    private void UpdateEach(int stock)
    {
            sw.WriteLine(ean);

    }

我的代码没有错误,但似乎没有全部写入我的文本文件.我没有创建sw.Close(),因为我知道某些线程尚未完成.另外,如果没有线程未完成,我该如何实现sw.Close.我的LUSTK.text中有5百万条记录,可供StreamReader读取,每条记录创建了一个线程,每个线程访问相同的文本文件.

I got no error on my code but it seems not all written to my text file. I did not make sw.Close() because i know some threads were not finish yet. In addition, how can i implement sw.Close knowing that no thread left unfinish. I have 5 milion records in my LUSTK.text that is to be read by StreamReader and each created a thread and each thread access same text file.

推荐答案

您将无法同时从不同线程向同一作者进行写操作.该对象并非旨在支持并发访问.

You aren't going to be able to concurrently write to the same writer from different threads. The object wasn't designed to support concurrent access.

除此之外,从多个线程写入同一文件的一般想法是有缺陷的.您仍然只有一个物理磁盘,并且它只能旋转得如此之快.告诉它更快地做事不会使它旋转得更快.

Beyond that, the general idea of writing to the same file from multiple threads is flawed. You still only have one physical disk, and it can only spin so fast. Telling it to do things faster won't make it spin any faster.

除此之外,您并没有按照您所说的那样关闭编写器,因此,缓冲区没有被刷新.

Beyond that, you're not closing the writer, as you said, and as a result, the buffer isn't being flushed.

您还有一个错误,即您的匿名方法正在关闭line,并且所有方法都正在关闭正在更改的同一变量.重要的是,它们每个都封闭自己不会改变的标识符. (这可以通过在while循环中声明line inside 来简单地完成.)但是由于您不应该使用多个线程作为开始,因此没有真正的重点.

You also have a bug in that your anonymous method is closing over line, and all of the methods are closing over the same variable, which is changing. It's important that they each close over their own identifier that won't change. (This can be accomplished simply by declaring line inside of the while loop.) But since you shouldn't be using multiple threads to begin with, there's no real need to focus on this.

您还可以使用File.ReadLinesFile.WriteAllLines进行文件IO;这样可以生成更简洁的代码:

You can also use File.ReadLines and File.WriteAllLines to do your file IO; it results in much cleaner code:

var values = File.ReadLines(inputFile)
    .Select(line => line.Split(';')[5]);
File.WriteAllLines(outputFile, values);

如果要并行化此过程,那是因为在读取该行之后并且在编写该行之前,您正在对每个项目进行一些CPU限制的工作.如前所述,将实际文件IO并行化可能有害,无济于事.在这种情况下,CPU限制工作只是拆分行并获取一个值,与文件IO相比,这可能出奇地快.例如,如果您需要访问数据库或在每一行上进行一些昂贵的处理,那么您可以考虑同时并行处理该部分工作,同时通过单个线程同步文件IO.

If you were to want to parallelize this process it would be because you're doing some CPU bound work on each item after you read the line and before you write the line. Parallelizing the actual file IO, as said before, is likely to be harmful, not helpful. In this case the CPU bound work is just splitting the line and grabbing one value, and that's likely to be amazingly fast compared to the file IO. If you needed to, for example, hit the database or do some expensive processing on each line, then you would consider parallelizing just that part of the work, while synchronizing the file IO through a single thread.

这篇关于StreamWriter多线程C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆