具体来说,将使用TPL Task.Factory.StartNew 产生一个线程:
Task.Factory.StartNew(()=> { File.ReadAllText(@thisFile.txt); });导致任何问题,等等?在 MSDN资源中似乎没有提到线程安全
它在SOAP Web服务环境中。
请不要了解在网络环境下使用任务的正反面。我完全了解这些问题, please ,理所当然,在我的情况下这个模型是合理的,谢谢。
解决方案没问题 - 假设没有任何文件同时写入,在这种情况下,您可能无法打开文件(或可能看到部分写入)。 b
根据文件的文件 : $ b
此类型的任何公共静态(Visual Basic中的Shared)成员都是线程安全的。任何实例成员不保证是线程安全的。
(不是说可以有任何实例方法,因为它是一个静态类。 ..)
Specifically will spawning a thread using the TPL Task.Factory.StartNew:
Task.Factory.StartNew(() => { File.ReadAllText(@"thisFile.txt"); });Causing any issues, etc? There doesn't appear to be any mention of thread safety on the MSDN resource
It's in a SOAP web service environment.
Ps Please, I don't want to know about the pro's and cons of using a Task in a web environment. I'm fully aware of these issues, please, just take it for granted that in my case this model is justified, thanks.
解决方案It's fine - assuming nothing's writing to the file at the same time, in which case you may not be able to open the file (or might see partial writes).
As per the documentation of File:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
(Not that there can be any instance methods, as it's a static class...)
更多推荐
File.ReadAllText线程安全吗?
发布评论