File.ReadAllText线程安全吗?

编程入门 行业动态 更新时间:2024-10-09 12:25:01
本文介绍了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线程安全吗?

本文发布于:2023-11-03 23:54:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   File   ReadAllText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!