如果由于程序崩溃而未关闭打开的 Filestream 会发生什么? [英] What happens to an open Filestream if it is not close due to program crash?

查看:31
本文介绍了如果由于程序崩溃而未关闭打开的 Filestream 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果由于程序崩溃而未关闭打开的 FileStream 会发生什么?会自动关闭吗?我无法将它包含在 try catch 或 using 中,因为我正在处理该文件.就像我使用打开"按钮打开文件一样.

What will happen to an open FileStream if it is not close due to a program crash? Will it be close automatically? I can't enclose it in a try catch or using because I am doing something with the file. It's like I open the file with an Open button.

private void button1_Click(object sender, RoutedEventArgs e)
{
    fs = new FileStream("Test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);          
}

然后我用它做点什么.然后在退出或关闭应用程序时我执行关闭.

Then I do something with it. Then in the Exit or closing of the application I execute close.

fs.Close();

但是如果中间发生了一些事情,比如程序崩溃怎么办.由于未正确关闭该文件是否将无法访问?谢谢.

But what if something happens in between, like a program crash. Will the file be not accessible since it is not close properly? Thanks.

推荐答案

很难找到当应用程序有意或无意退出时 Windows 会做什么.

It's hard to find exactly what Windows will do when an application exits, either intentionally or otherwise.

终止进程 MSDN 上的文章描述了进程终止时会发生什么,最值得注意的是.

The Terminating a Process article on MSDN describes what happens when a process is terminated, most notably.

进程中的任何剩余线程都被标记为终止.
进程分配的任何资源都将被释放.
所有内核对象都已关闭.
进程代码从内存中删除.
进程退出代码已设置.
进程对象被发送信号.

Any remaining threads in the process are marked for termination.
Any resources allocated by the process are freed.
All kernel objects are closed.
The process code is removed from memory.
The process exit code is set.
The process object is signaled.

有点跑题,但在您的特定代码段中,FileStream 实现了 IDisposable,因此您应该研究 使用关键字获取有关确定性资源清理的信息.

Slightly off-topic, but in your particular piece of code, FileStream implements IDisposable, so you should research on the using keyword for information about deterministic resource clean-up.

这篇关于如果由于程序崩溃而未关闭打开的 Filestream 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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