Skip to content

Commit

Permalink
Merge pull request #4076 from Jan18101997/develop
Browse files Browse the repository at this point in the history
Fixed issue #4072
  • Loading branch information
punker76 committed Apr 20, 2021
1 parent b72af9e commit 11a5fa5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
window.Closing += this.AssociatedObject_Closing;
window.Closed += this.AssociatedObject_Closed;

// This operation must be thread safe
window.BeginInvoke(() =>
// This operation must be thread safe. It is possible, that the window is running in a different Thread.
Application.Current?.BeginInvoke(app =>
{
var application = Application.Current;
if (application != null)
if (app != null)
{
application.SessionEnding += this.CurrentApplicationSessionEnding;
app.SessionEnding += this.CurrentApplicationSessionEnding;
}
});
});
}

private void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e)
Expand Down Expand Up @@ -100,12 +99,11 @@ private void CleanUp(string fromWhere)
window.SourceInitialized -= this.AssociatedObject_SourceInitialized;

// This operation must be thread safe
window.BeginInvoke(() =>
Application.Current?.BeginInvoke(app =>
{
var application = Application.Current;
if (application != null)
if (app != null)
{
application.SessionEnding -= this.CurrentApplicationSessionEnding;
app.SessionEnding -= this.CurrentApplicationSessionEnding;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Controls/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void BeginInvoke([NotNull] this DispatcherObject dispatcherObject,
throw new ArgumentNullException(nameof(invokeAction));
}

dispatcherObject.Dispatcher.BeginInvoke(priority, invokeAction);
dispatcherObject.Dispatcher?.BeginInvoke(priority, invokeAction);
}

public static void BeginInvoke<T>([NotNull] this T dispatcherObject, [NotNull] Action<T> invokeAction, DispatcherPriority priority = DispatcherPriority.Background)
Expand Down

0 comments on commit 11a5fa5

Please sign in to comment.