From 7ba082c3f261820ad015f3143d02426c4f73041c Mon Sep 17 00:00:00 2001 From: tangge233 Date: Sun, 13 Oct 2024 21:33:10 +0800 Subject: [PATCH] chore: apply suggestions --- Plain Craft Launcher 2/Controls/MyImage.vb | 45 ++++++++++++------- .../Pages/PageLaunch/PageLaunchRight.xaml | 1 + 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Plain Craft Launcher 2/Controls/MyImage.vb b/Plain Craft Launcher 2/Controls/MyImage.vb index b720fe1c..93f1588d 100644 --- a/Plain Craft Launcher 2/Controls/MyImage.vb +++ b/Plain Craft Launcher 2/Controls/MyImage.vb @@ -7,7 +7,6 @@ Public Class MyImage Private _SourceData As String = "" Private _UseCache As Boolean = False - Private _DownloadTask As Task Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 @@ -35,9 +34,6 @@ Public Class MyImage MyBase.Source = New MyBitmap(_SourceData) Exit Property End If - If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 - _DownloadTask.Dispose() - End If Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 @@ -45,18 +41,9 @@ Public Class MyImage MyBase.Source = New MyBitmap(TempFilePath) Exit Property End If - ' 异步下载图片 - Dim TaskID = 0 - _DownloadTask = New Task(Sub() - NetDownload(_SourceData, TempFilePath, True) - End Sub) - TaskID = _DownloadTask.Id - _DownloadTask.Start() - _DownloadTask.ContinueWith(Sub(t) - If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 - MyBase.Source = New MyBitmap(TempFilePath) - End If - End Sub, TaskScheduler.FromCurrentSynchronizationContext()) + ' 开一个线程处理在线图片 + RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) + Catch ex As Exception Log(ex, "加载图片失败") End Try @@ -70,4 +57,30 @@ Public Class MyImage End If End Sub))) + Private Sub PicLoader(FileUrl As String, TempFilePath As String) + Dim Retried As Boolean = False +RetryStart: + Try + If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片 + Rename(TempFilePath, TempFilePath & ".old") + RunInUi(Sub() + MyBase.Source = New MyBitmap(TempFilePath & ".old") + End Sub) + End If + NetDownload(FileUrl, TempFilePath) + RunInUi(Sub() + MyBase.Source = New MyBitmap(TempFilePath) + End Sub) + File.Delete(TempFilePath & ".old") + Catch ex As Exception + If Not Retried Then + Retried = True + GoTo RetryStart + Else + Log(ex, $"[MyImage] 下载图片失败") + RunInUi(Sub() MyBase.Source = New MyBitmap(PathImage & "Icons/NoIcon.png")) + End If + End Try + End Sub + End Class diff --git a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml index 1cc6a708..9c02a492 100644 --- a/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml +++ b/Plain Craft Launcher 2/Pages/PageLaunch/PageLaunchRight.xaml @@ -6,6 +6,7 @@ xmlns:local="clr-namespace:PCL"> +