如果打开的Filestream由于程序崩溃而没有关闭,将会如何处理? [英] What happens to an open Filestream if it is not close due to program crash?

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

问题描述

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

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);          
}

然后我要对其进行处理。然后在应用程序的退出或关闭中执行close。

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天全站免登陆