[Mono-dev] Problems with FileStream.Lock();

Rafael Teixeira monoman at gmail.com
Tue Jan 8 16:54:40 UTC 2013


I'm not sure, but I think locking is unavailable on Android, and so Mono
can't build up on the abstraction...

Rafael "Monoman" Teixeira
---------------------------------------
"The most exciting phrase to hear in science, the one that heralds new
discoveries, is not 'Eureka!' (I found it!) but 'That's funny ...'"
Isaac Asimov
US science fiction novelist & scholar (1920 - 1992)


On Mon, Jan 7, 2013 at 7:50 PM, Terry-Watts.com <Terry at terry-watts.com>wrote:

>  I have a class that work fine in C# under Windows but not under
> Monodroid.
>
> A simplified version of the class is:
>
> public **class LockedStream : FileStream{
>     ulong LockCount = 0;
>     bool _IsNewFile;
>
>       LockedStream( bool IsNewFile, string path, FileMode mode, FileAccess access, FileShare share )
>                   : base( MakePath( path ), mode, access, share )
>          {
>            _IsNewFile = IsNewFile;
>          }
>
>    public LockedStream( string path, FileMode mode, FileAccess access, FileShare share )
>                   : this( !File.Exists( path ), path, mode, access, share )
>          {
>          }
>
>    public static internal LockedStream Open( string FilePathName )
>         {
>            return ( new LockedStream( FilePathName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite ) );
>         }
>
>     public void Lock()
>         {
>             if( LockCount++ == 0 )
>             {
>                  int SleepCount = 1;
>
>                     while( true )
>                     {
>                         try
>                         {
>                             base.Lock( 0, Int64.MaxValue );
>                             break;
>                          }
>                          catch( Exception E )
>                          {
>                               Thread.Sleep( SleepCount++ );
>                          }
>                      }
>              }
>         }
>
>    public void UnLock()
>           {
>               if( LockCount > 0 )
>               {
>                   if( --LockCount == 0 )
>                   {
>                       try
>                       {
>                           base.Unlock( 0, Int64.MaxValue );
>                       }
>                       catch( Exception )
>                       {
>                       }
>                    }
>                }
>            }
> }
>
> Now when I do:
>
> LockedStream Stream = LockedStream.Open( "Some Path/file name" );
>      Stream.Lock();
>
> I get the file created when I call the Open(); but when I call
> Stream.Lock(); I hangs in the lock routine forever because base.Lock( 0,
> Int64.MaxValue ); throws a lock violation exception even though the file
> isn't locked.
>
> Does anybody know why this is happening on Android?
>
> Thanks in advance.
>
> Terry
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130108/fdbeaabb/attachment-0001.html>


More information about the Mono-devel-list mailing list