Skip to content

Commit

Permalink
chore: Try Catch
Browse files Browse the repository at this point in the history
  • Loading branch information
tangge233 committed Oct 8, 2024
1 parent 71d915f commit 50112b7
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions Plain Craft Launcher 2/Controls/MyImage.vb
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,36 @@ Public Class MyImage
Set(value As String)
If String.IsNullOrEmpty(value) Then Exit Property
_SourceData = value
If Not value.StartsWithF("http") Then ' 本地资源直接使用
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 ' 缓存文件存在且未过期,不需要重下
If Not NeedDownload Then
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())
Try
If Not value.StartsWithF("http") Then ' 本地资源直接使用
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 ' 缓存文件存在且未过期,不需要重下
If Not NeedDownload Then
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())
Catch ex As Exception
Log(ex, "加载图片失败")
End Try
End Set
End Property
Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(
Expand Down

0 comments on commit 50112b7

Please sign in to comment.