在C#中使用线程或队列写入文件

我在下面的代码中花费了很多时间。我在想有4个线程将从文件中读取ID,然后转储由ConnectToServiceAndDump完成的详细信息。由于所有ID都是唯一的,所以我可以4个线程接4个线程唯一ID,并调用ConnectToServiceAndDump。我已经尝试了Threading类将线程数设置为4,但是如果我将它保留在while循环中是行不通的。


或类似这样的方法:


//- create a list.. which contains all the ids

//- create a new thread 

//- in a theard safe way pop a loan from list    

//- dump this id by calling service in the same thread 

//- use ConcurrentQueue(T)  and Enqueue


using (StreamReader sr = new StreamReader($@"{File}"))

{

    var ids = sr.ReadLine();

    while (ids != null)

    {                                

        ConnectToServiceAndDump(client, ids, outputSubdirectoryName);

        ids = sr.ReadLine();

    }

}


holdtom
浏览 275回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP