使用具有多重刻划的streamwriter [英] using streamwriter with multithrerading
问题描述
我有一个场景,我有4个线程,必须在每个线程完成后使用streamwriter将文本写入文件(第一个线程触发必须写第一)。有人可以帮助我用线程来实现这一点。
I have a scenario where I have 4 threads which has to write text to a file using streamwriter after completion of each thread(first thread triggered has to write first). Could someone please help me to achieve this with threads.
谢谢
推荐答案
考虑下一个接近。每个线程都将执行:
lock(myobj)
{
使用(var sw = new StreamWriter("myFile.txt",append:true))
using( var sw = new StreamWriter( "myFile.txt", append: true ) )
  
{
   
使用'sw'写入文件。 。 。
write to file using 'sw'. . .
}
< span lang ="EN-GB"style ="margin:0px"> }
其中
myobj 是一个唯一的对象,在任何类中定义:
where myobj is a unique object, defined in any class:
public static object myobj = new object();
您可以创建一个公共或静态函数。
另请参阅
File.AppendXXX 函数。
这篇关于使用具有多重刻划的streamwriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!