Skip to content

Commit

Permalink
修复线程上下文
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Feb 25, 2024
1 parent e09b408 commit 3b9e016
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
10 changes: 5 additions & 5 deletions CoreAppUWP.Projection/CoreAppUWP.Projection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dongle.WindowsSDK.NET" Version="10.0.22621.756" PrivateAssets="All"/>
<PackageReference Include="Dongle.Windows.CsWinRT" Version="3.0.0-wux.2"/>
<PackageReference Include="Microsoft.UI.Xaml" Version="2.8.6" PrivateAssets="All">
<ExcludeAssets>build; buildtransitive; compile; runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Dongle.WindowsSDK.NET" Version="10.0.22621.756" PrivateAssets="All"/>
<PackageReference Include="Dongle.Windows.CsWinRT" Version="3.0.0-wux.2"/>
<PackageReference Include="Microsoft.UI.Xaml" Version="2.8.6" PrivateAssets="All">
<ExcludeAssets>build; buildtransitive; compile; runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 7 additions & 5 deletions CoreAppUWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs e)

private void EnsureWindow(IActivatedEventArgs e)
{
if (Window.Current is not Window window) { return; }

if (SynchronizationContext.Current == null)
{
SynchronizationContext.SetSynchronizationContext(new CoreDispatcherSynchronizationContext(window.Dispatcher));
}

if (!isLoaded)
{
if (SynchronizationContext.Current == null)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
}
RegisterExceptionHandlingSynchronizationContext();
SettingsHelper.CreateLogManager();
isLoaded = true;
}

if (Window.Current is not Window window) { return; }
WindowHelper.TrackWindow(window);

// 不要在窗口已包含内容时重复应用程序初始化,
Expand Down
10 changes: 10 additions & 0 deletions CoreAppUWP/Common/ExceptionHandling.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;

namespace CoreAppUWP.Common
Expand Down Expand Up @@ -131,6 +132,15 @@ private bool HandleException(Exception exception)
public event EventHandler<UnhandledExceptionEventArgs> UnhandledException;
}

public sealed class CoreDispatcherSynchronizationContext(CoreDispatcher dispatcher) : SynchronizationContext
{
public override void Post(SendOrPostCallback d, object state) =>
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => d?.Invoke(state));

public override void Send(SendOrPostCallback d, object state) =>
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => d?.Invoke(state));
}

public class UnhandledExceptionEventArgs : EventArgs
{
public bool Handled { get; set; }
Expand Down

0 comments on commit 3b9e016

Please sign in to comment.