简体   繁体   English

如果应用程序由于未处理的异常或其他原因而崩溃,FileStream会发生什么?

[英]What happens to a FileStream if an application crashes via an unhandled exception or otherwise?

I have an application made to be accessed by multiple users at the same time and leverages an API that uses OAuth 2.0. 我有一个可供多个用户同时访问的应用程序,它利用了使用OAuth 2.0的API。 The application depends on a single file that stores information such as the access token for making API calls. 该应用程序依赖一个文件来存储信息,例如用于进行API调用的访问令牌。 When an access token expires, the app must go through the process of getting a new one via the API and save it to the file. 当访问令牌过期时,应用必须经历通过API获取新令牌的过程,并将其保存到文件中。 If multiple people attempt to do this at once, only one of those tokens will work. 如果多个人尝试一次执行此操作,则这些令牌中只有一个起作用。

The proposed solution was to open a FileStream to a specific file with only Read access when a user starts the process of getting a new token. 提议的解决方案是在用户开始获取新令牌的过程时,打开仅具有读取访问权限的特定文件的FileStream。 That FileStream will then be disposed of when the process ends. 然后,该FileStream将在处理结束时被丢弃。 The app will check if that file can be written to. 该应用程序将检查该文件是否可以写入。 If it cannot, that means someone else is getting a new token and that instance of the application must wait until the new token has been obtained. 如果不能,则意味着其他人正在获取新令牌,并且该应用程序实例必须等待,直到获得新令牌。

If the application crashes during that process, what happens to the lock on that file? 如果应用程序在该过程中崩溃,那么对该文件的锁定将如何处理? I assume the operating system frees that lock, but what if the crash happened due to something in unmanaged code or something else that wasn't an unhandled exception? 我假设操作系统会释放该锁,但是如果崩溃是由于非托管代码中的某些原因或不是未处理的异常的其他原因而发生的,该怎么办?

ok the thing is when you open a file it has these modes. 好吧,当您打开文件时,它具有这些模式。 and whenever you open a file a handler is created and pass it to you and you assign it to a ref 每当您打开文件时,都会创建一个处理程序并将其传递给您,然后将其分配给引用

  1. reading
  2. writing 写作
  3. both of them 他们都
  4. neither of those 那些都不

An open file that is not shared it cannot be open in the same caller application or other until it closed by caller app and it's exclusive access. 未共享的打开文件只有在被调用方应用程序关闭且具有独占访问权之后,才能在同一调用方应用程序或其他调用方应用程序中打开。

If file open in sharing mode the system compares the requested access and sharing modes to those specified when the file was opened. 如果文件以共享模式打开,则系统会将请求的访问和共享模式与打开文件时指定的访问和共享模式进行比较。 If you specify an access or sharing mode that conflicts with the modes specified in the previous call, open file fails. 如果您指定的访问或共享模式与上一个调用中指定的模式冲突,则打开文件失败。

our application experiences a hard crash your managed code's resources will not get cleared by application or it will not try to recover the things . 我们的应用程序遇到严重崩溃,您的托管代码资源将不会被应用程序清除,也不会尝试恢复它们。 basically all the GC things of your code will not run.but the operating system will still attempt to clean up after you. 基本上,代码中的所有GC内容都不会运行。但是,操作系统在您执行之后仍会尝试清除。 This solves the problem of unreleased memory, handles, and other system objects so does your FileStream handlers 这解决了未释放的内存,句柄和其他系统对象的问题,您的FileStream处理程序也解决了

My best guess is that if file is in shared mode lock on that file will be release by the OS cleanup after your app crashes. 我最好的猜测是,如果文件处于共享模式,则应用崩溃后,操作系统清理将释放该文件上的锁定。 cus your app's logic will never reach to that state again. 请注意,您的应用程序逻辑将永远不会再达到该状态。

this is also true with un-managed code segments cus it directly calling the Managed OS api and the OS handlers will create and if that os handlers in a crash those handlers will be takeen care by OS it self. 对于非托管代码段也是如此,因为它直接调用Managed OS api,并且OS处理程序将创建,并且如果os处理程序在崩溃时,这些处理程序将由OS自行处理。

this is how Windows 10 prevent memory location hijacking on it's system file locations. 这就是Windows 10如何防止内存位置在其系统文件位置上被劫持的方式。

If you are using any unmanaged code and exception comes in middle of that. 如果您使用的是任何非托管代码,那么中间就会出现异常。 System will leave the file in use. 系统将保留该文件的使用状态。 That's why in our code it is always a best practice to use either 'using' block for that OR try {} catch {} and finally {}. 这就是为什么在我们的代码中始终使用“ using”块或尝试{} catch {}并最终{}始终是最佳实践的原因。

using statement will make sure to release the resources on it's own while you need to explicitly release them in try , catch and finally block, once the object goes out of scope. 当对象超出范围时,using语句将确保释放资源,而您需要在try,catch和finally块中显式释放资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果发生未处理的异常,我需要关闭WPF应用程序吗? - If unhandled exception happens I need to shutdown the WPF Application? 当Azure计划的WebJob抛出未处理的异常时会发生什么? - What happens when an Azure Scheduled WebJob throws an unhandled exception? 如果我的 Class 库中存在未处理的异常,我的应用程序会崩溃吗? - My Application crashes if there is an unhandled exception in my Class Library? 使用sql服务代理,如果目标应用程序崩溃怎么办? - With sql service broker what happens if the target application crashes? WPF应用程序崩溃,并显示“ WindowsBase.dll中发生了'System.ComponentModel.Win32Exception类型的未处理的异常” - WPF application crashes with “An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in WindowsBase.dll” 未处理的异常会中断应用程序吗? - An unhandled exception breaks the application? FileStream.copyTo(Net.ConnectStream)实习生会发生什么? - FileStream.copyTo(Net.ConnectStream) what happens intern? 当文件被其他进程删除时,文件流会发生什么? - What happens to a filestream when the file is deleted by a different process? Windows Service应用程序中未处理的异常 - unhandled exception in windows service application 在未处理的异常之后终止应用程序 - Terminate application after unhandled exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM