.NET Isolated NRE Lock Lock File

Therefore, I am trying to lock an isolated storage file in my C # client application, so that multiple instances of my application will not be able to access it at the same time. I use the following syntax:

lockStream = new IsolatedStorageFileStream("my.lck", FileMode.OpenOrCreate, isoStore); lockStream.Lock(0, 0); 

This code causes my application to throw a NullReferenceException from the FileStream.Lock method of the structure. I tried to use a non-zero value for the length. I tried to write a byte to a file, and then blocked only that byte. No matter what I do, the same NullReferenceException haunts me. Does anyone know if this is possible with isolated storage?

I also learn this technique in a Silverlight application, does Silverlight support file locking? MSDN docs seem to indicate that this is not the case, but I saw this post from C # MVP which says it does.

Update: Microsoft fixed an error that I sent to Connect, but it was not released in version 4 from the framework. It should be available, hopefully in the next SP or full release.

+4
source share
2 answers

It looks like a bug in the Framework. Maybe I'm wrong because it is really too big to be true.

Looking at the source code for .NET 3.5 SP1 with Reflector, you will find that IsolStorageFileStream calls the dimensionless base constructor (FileStream ()), which leads to a non-initialized base class. IsolatedStorageFileStream creates an instance of FileStream and uses it in all methods that it overrides (Write, Read, Flush, Seek, etc.). It is strange that it does not directly use its base class.

But the lock and unlock are not overridden, and they need a private field (_handle), which is still null (since the constructor used is parameterless). They assume that it is non-zero and casts it and calls NRE.

To summarize, locking and unlocking are not supported (or do not work).

I think you are forced to use other locking methods, such as Mutex or Semaphore.

The implementation is the same in .NET 4 RC. In Silverlight, there is no unlock lock at all (my apologies for the conflict with MVP).

+4
source

Try to have a value greater than 0 for the amount of data being blocked. Also, is there any data in the stream if there is nothing to block, which could be a problem ....

  lockStream = new IsolatedStorageFileStream("my.lck", FileMode.OpenOrCreate, isoStore); lockStream.Write(.....) lockStream.Lock(0, 10); 
0
source

Source: https://habr.com/ru/post/1303169/


All Articles