c# - File.ReadAllText 线程安全吗?

标签 c# web-services file-io task-parallel-library

特别是使用 TPL Task.Factory.StartNew 生成一个线程:

Task.Factory.StartNew(() => {
       File.ReadAllText(@"thisFile.txt");
});

导致任何问题等? MSDN resource 上似乎没有提到线程安全。

它在 SOAP 网络服务环境中。

请注意,我不想了解在网络环境中使用任务的优缺点。我完全了解这些问题,,只是想当然地认为在我的情况下这个模型是合理的,谢谢。

最佳答案

没关系 - 假设同时没有任何内容写入文件,在这种情况下您可能无法打开文件(或者可能看到部分写入)。

根据 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.

(并不是说可以有任何实例方法,因为它是一个静态类...)

关于c# - File.ReadAllText 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976449/

相关文章:

c# - 路由声明它在 system.web 中定义但未找到错误

web-services - 邮政编码 + 国家/地区到地理坐标

java - 如何修复 wsdl 文件错误?

c++ - String Game,创建一个特定的循环? C++

c++ - 多线程和获取错误调用函数

c# - 为什么 C# 不支持 base.base?

c# - 为什么用我自己的类的实例调用 DynamicMethod 会导致异常?

c# - StateHasChanged() 之后 Blazor 组件未更新

web-services - 如何使用SQL Server 2008 Reporting Services的WebService

java - 从文件中确定最小和最大 double 值