System.IO.MemoryMappedFiles.MemoryMappedView.Flush C# (CSharp) Méthode

Flush() private méthode

private Flush ( UIntPtr capacity ) : void
capacity System.UIntPtr
Résultat void
        public void Flush(UIntPtr capacity)
        {
            unsafe
            {
                byte* firstPagePtr = null;
                try
                {
                    _viewHandle.AcquirePointer(ref firstPagePtr);

                    bool success = Interop.Kernel32.FlushViewOfFile((IntPtr)firstPagePtr, capacity) != 0;
                    if (success)
                        return; // This will visit the finally block.

                    // It is a known issue within the NTFS transaction log system that
                    // causes FlushViewOfFile to intermittently fail with ERROR_LOCK_VIOLATION
                    // As a workaround, we catch this particular error and retry the flush operation 
                    // a few milliseconds later. If it does not work, we give it a few more tries with
                    // increasing intervals. Eventually, however, we need to give up. In ad-hoc tests
                    // this strategy successfully flushed the view after no more than 3 retries.

                    int error = Marshal.GetLastWin32Error();
                    bool canRetry = (!success && error == Interop.Errors.ERROR_LOCK_VIOLATION);

                    SpinWait spinWait = new SpinWait();
                    for (int w = 0; canRetry && w < MaxFlushWaits; w++)
                    {
                        int pause = (1 << w);  // MaxFlushRetries should never be over 30
                        MemoryMappedFile.ThreadSleep(pause);

                        for (int r = 0; canRetry && r < MaxFlushRetriesPerWait; r++)
                        {
                            success = Interop.Kernel32.FlushViewOfFile((IntPtr)firstPagePtr, capacity) != 0;
                            if (success)
                                return; // This will visit the finally block.

                            spinWait.SpinOnce();

                            error = Marshal.GetLastWin32Error();
                            canRetry = (error == Interop.Errors.ERROR_LOCK_VIOLATION);
                        }
                    }

                    // We got to here, so there was no success:
                    throw Win32Marshal.GetExceptionForWin32Error(error);
                }
                finally
                {
                    if (firstPagePtr != null)
                    {
                        _viewHandle.ReleasePointer();
                    }
                }
            }
        }

Usage Example

Exemple #1
0
        // Flushes the changes such that they are in sync with the FileStream bits (ones obtained
        // with the win32 ReadFile and WriteFile functions).  Need to call FileStream's Flush to
        // flush to the disk.
        // NOTE: This will flush all bytes before and after the view up until an offset that is a
        // multiple of SystemPageSize.
        public override void Flush()
        {
            if (!CanSeek)
            {
                throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamIsClosed);
            }

            _view.Flush((UIntPtr)Capacity);
        }
All Usage Examples Of System.IO.MemoryMappedFiles.MemoryMappedView::Flush