8000 Try fix WriteableBitmap hangs when source bitmap is rendered on other… · dotnet/wpf@15b303f · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 15b303f

Browse files
committed
Try fix WriteableBitmap hangs when source bitmap is rendered on other thread
#4396
1 parent 263c65e commit 15b303f

File tree

1 file changed

+34
-13
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging

1 file changed

+34
-13
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ ref lockBufferStride
300300
/// on the back buffer and request a render pass.
301301
/// </summary>
302302
public void Unlock()
303+
{
304+
UnlockWithoutSubscribeToCommittingBatch();
305+
SubscribeToCommittingBatchAndWritePostscript();
306+
}
307+
308+
private Void UnlockWithoutSubscribeToCommittingBatch()
303309
{
304310
WritePreamble();
305311

@@ -315,16 +321,19 @@ public void Unlock()
315321
// This makes the back buffer read-only.
316322
_pBackBufferLock.Dispose();
317323
_pBackBufferLock = null;
324+
}
325+
}
318326

319-
if (_hasDirtyRects)
320-
{
321-
SubscribeToCommittingBatch();
327+
private Void SubscribeToCommittingBatchAndWritePostscript()
328+
{
329+
if (_hasDirtyRects)
330+
{
331+
SubscribeToCommittingBatch();
322332

323-
//
324-
// Notify listeners that we have changed.
325-
//
326-
WritePostscript();
327-
}
333+
//
334+
// Notify listeners that we have changed.
335+
//
336+
WritePostscript();
328337
}
329338
}
330339

@@ -723,17 +732,19 @@ BitmapSource source
723732

724733
BeginInit();
725734

726-
_syncObject = source.SyncObject;
727-
lock (_syncObject)
735+
// We will change the _syncObject object in Lock()
736+
var syncObject = _syncObject = source.SyncObject;
737+
try
728738
{
739+
Monitor.Enter(syncObject);
729740
Guid formatGuid = source.Format.Guid;
730741

731742
SafeMILHandle internalPalette = new SafeMILHandle();
732743
if (source.Format.Palettized)
733744
{
734745
internalPalette = source.Palette.InternalPalette;
735746
}
736-
747+
737748
HRESULT.Check(MILSwDoubleBufferedBitmap.Create(
738749
(uint)source.PixelWidth, // safe cast
739750
(uint)source.PixelHeight, // safe cast
@@ -742,7 +753,7 @@ BitmapSource source
742753
ref formatGuid,
743754
internalPalette,
744755
out _pDoubleBufferedBitmap
745-
));
756+
));
746757

747758
_pDoubleBufferedBitmap.UpdateEstimatedSize(
748759
GetEstimatedSize(source.PixelWidth, source.PixelHeight, source.Format));
@@ -758,7 +769,17 @@ out _pDoubleBufferedBitmap
758769
}
759770
finally
760771
{
761-
Unlock();
772+
UnlockWithoutSubscribeToCommittingBatch();
773+
Monitor.Exit(syncObject);
774+
}
775+
776+
SubscribeToCommittingBatchAndWritePostscript();
777+
}
778+
finally
779+
{
780+
if (Monitor.IsEntered(syncObject))
781+
{
782+
Monitor.Exit(syncObject);
762783
}
763784
}
764785

0 commit comments

Comments
 (0)
0