Skip to content

Commit

Permalink
-修复关闭文件时没有正常终止后台进程的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Aug 25, 2023
1 parent 51e6b66 commit 4adff9b
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions App/Functions/Editor/PdfViewerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SelectionChangedEventArgs(Editor.Selection selection) {

readonly BackgroundWorker _renderWorker;
readonly Timer _refreshTimer;
bool _cancelRendering;
bool _cancelRendering, _disposed;
bool _lockDown;
MuDocument _mupdf;
readonly ImageRendererOptions _renderOptions;
Expand Down Expand Up @@ -342,12 +342,13 @@ public PdfViewerControl() {
Interval = 200
};
_refreshTimer.Tick += (s, args) => {
for (int i = _DisplayRange.StartValue; i <= _DisplayRange.EndValue; i++) {
var r = _DisplayRange;
for (int i = r.StartValue; i <= r.EndValue; i++) {
bool v;
lock (_cache.SyncObj) {
v = _cache.GetBitmap(i) != null;
}
if (v == false && _renderWorker.IsBusy == false) {
if (v == false && _disposed == false && _renderWorker.IsBusy == false) {
_renderWorker.RunWorkerAsync();
return;
}
Expand All @@ -358,9 +359,13 @@ public PdfViewerControl() {
WorkerSupportsCancellation = true
};
_renderWorker.DoWork += (s, args) => {
Tracker.DebugMessage("started prerender job: " + _DisplayRange);
var r = _DisplayRange;
Tracker.DebugMessage("started prerender job: " + r);
_refreshTimer.Stop();
for (int i = _DisplayRange.StartValue; i >= _DisplayRange.StartValue && i < _DisplayRange.EndValue + 2; i++) {
if (_disposed) {
return;
}
for (int i = r.StartValue; i >= r.StartValue && i < r.EndValue + 2; i++) {
if (i < 1 || i > _mupdf.PageCount) {
continue;
}
Expand All @@ -375,15 +380,15 @@ public PdfViewerControl() {
Tracker.DebugMessage("load page " + i);
var z = GetZoomFactorForPage(pb);
RenderPage(i, (pb.Width * z).ToInt32(), (pb.Height * z).ToInt32());
if (_DisplayRange.Contains(i)) {
if (r.Contains(i)) {
Invalidate();
}
}
}
}
};
_renderWorker.RunWorkerCompleted += (s, args) => {
if (_cancelRendering == false) {
if (_cancelRendering == false && _disposed == false) {
_refreshTimer.Start();
}
};
Expand Down Expand Up @@ -946,7 +951,7 @@ bool CalculateZoomFactor(string zoomMode) {

void UpdateDisplay() { UpdateDisplay(false); }
void UpdateDisplay(bool resized) {
if (DesignMode) {
if (DesignMode || _disposed) {
return;
}
_refreshTimer.Stop();
Expand Down Expand Up @@ -1236,10 +1241,11 @@ protected override void Dispose(bool disposing) {
base.Dispose(disposing);
Tracker.DebugMessage("PDF Viewer control destroyed.");
_cancelRendering = true;
_disposed = true;
_mupdf?.AbortAsync();
_refreshTimer.Stop();
_renderWorker.CancelAsync();
if (_cache != null) {
_renderWorker.CancelAsync();
lock (_cache.SyncObj) {
_cache.Dispose();
}
Expand Down

0 comments on commit 4adff9b

Please sign in to comment.