Skip to content

Commit

Permalink
Fix IOSharingViolation error for HomePage.TryLoadEventPanelImage
Browse files Browse the repository at this point in the history
  • Loading branch information
bagusnl committed Nov 24, 2024
1 parent 5f5233e commit c9cd14d
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Media;
using System.Collections.Concurrent;
using static CollapseLauncher.Dialogs.SimpleDialogs;
using static CollapseLauncher.InnerLauncherConfig;
using static CollapseLauncher.Helper.Background.BackgroundMediaUtility;
Expand Down Expand Up @@ -295,6 +296,7 @@ private void Page_Unloaded(object sender, RoutedEventArgs e)
#endregion

#region EventPanel
private ConcurrentDictionary<string, byte> _eventPanelProcessing = new();
private async void TryLoadEventPanelImage()
{
// Get the url and article image path
Expand All @@ -303,6 +305,14 @@ private async void TryLoadEventPanelImage()
string featuredEventIconImg = LauncherMetadataHelper.CurrentMetadataConfig?.GameLauncherApi?
.LauncherGameNews?.Content?.Background?.FeaturedEventIconBtnImg;

if (!_eventPanelProcessing.TryAdd(featuredEventArticleUrl, 0) ||
!_eventPanelProcessing.TryAdd(featuredEventIconImg, 0))
{
LogWriteLine($"[TryLoadEventPanelImage] Stopped processing {featuredEventIconImg} and {featuredEventArticleUrl} due to double processing",
LogType.Warning, true);
return;
}

// If the region event panel property is null, then return
if (string.IsNullOrEmpty(featuredEventIconImg)) return;

Expand All @@ -327,22 +337,23 @@ private async void TryLoadEventPanelImage()
{
// Using the original icon file and cached icon file streams
if (!isCacheIconExist)
await using (Stream cachedIconFileStream = cachedIconFileInfo.Create())
await using (FileStream cachedIconFileStream = new FileStream(cachedIconFileInfo.FullName,
FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
await using (Stream copyIconFileStream = new MemoryStream())
{
await using (Stream iconFileStream =
await FallbackCDNUtil.GetHttpStreamFromResponse(featuredEventIconImg,
PageToken.Token))
PageToken.Token))
{
var scaleFactor = WindowUtility.CurrentWindowMonitorScaleFactor;
// Copy remote stream to memory stream
await iconFileStream.CopyToAsync(copyIconFileStream);
copyIconFileStream.Position = 0;
// Get the icon image information and set the resized frame size
var iconImageInfo = await Task.Run(() => ImageFileInfo.Load(copyIconFileStream));
var width = (int)(iconImageInfo.Frames[0].Width * scaleFactor);
var height = (int)(iconImageInfo.Frames[0].Height * scaleFactor);
var width = (int)(iconImageInfo.Frames[0].Width * scaleFactor);
var height = (int)(iconImageInfo.Frames[0].Height * scaleFactor);

copyIconFileStream.Position = 0; // Reset the original icon stream position
await ImageLoaderHelper.ResizeImageStream(copyIconFileStream, cachedIconFileStream,
Expand All @@ -363,14 +374,19 @@ await ImageLoaderHelper.ResizeImageStream(copyIconFileStream, cachedIconFileStre

// Set event icon props
ImageEventImgGrid.Visibility = !NeedShowEventIcon ? Visibility.Collapsed : Visibility.Visible;
ImageEventImg.Source = source;
ImageEventImg.Tag = featuredEventArticleUrl;
ImageEventImg.Source = source;
ImageEventImg.Tag = featuredEventArticleUrl;
}
catch (Exception ex)
{
await SentryHelper.ExceptionHandlerAsync(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed while loading EventPanel image icon\r\n{ex}", LogType.Error, true);
}
finally
{
_eventPanelProcessing.Remove(featuredEventIconImg, out _);
_eventPanelProcessing.Remove(featuredEventArticleUrl, out _);
}
}
#endregion

Expand Down

0 comments on commit c9cd14d

Please sign in to comment.