File.ReadAllText线程安全吗? [英] Is File.ReadAllText thread safe?

查看:238
本文介绍了File.ReadAllText线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,将使用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线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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