Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bypass a file lock in C# when another thread/process is unecessarily using an exclusive lock?

Tags:

c#

.net

file

Is there a way to bypass or remove the file lock held by another thread without killing the thread?

I am using a third-party library in my app that is performing read-only operations on a file. I need a second thread read the file at the same time to extract out some extra data the third-party library is not exposing. Unfortunately, the third-party library opened the file using a Read/Write lock and hence I am getting the usual "The process cannot access the file ... because it is being used by another process" exception.

I would like to avoid pre-loading the entire file with my thread because the file is large and would cause unwanted delays in the loading of this file and excess memory usage. Copying the file is not practical due to the size of the files. During normal operation, two threads hitting the same file would not cause any significant IO contention/performance problems. I don't need perfect time-synchronization between the two threads, but they need to be reading the same data within a half second of eachother.

I cannot change the third-party library.

Are there any work-arounds to this problem?

like image 644
James Schek Avatar asked Dec 29 '09 23:12

James Schek


1 Answers

If you start messing with the underlying file handle you may be able to unlock portions, the trouble is that the thread accessing the file is not designed to handle this kind of tampering and may end up crashing.

My strong recommendation would be to patch the third party library, anything you do can and probably will blow up in real world conditions.

like image 190
Sam Saffron Avatar answered Sep 30 '22 07:09

Sam Saffron