diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index a5ec0b56c..caf5d0a48 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -5,8 +5,6 @@ using System.Text.RegularExpressions; using System.Threading; -using Avalonia.Threading; - namespace SourceGit.Commands { public partial class Command @@ -68,7 +66,7 @@ public bool Exec() catch (Exception e) { if (RaiseError) - Dispatcher.UIThread.Post(() => App.RaiseException(Context, e.Message)); + App.RaiseException(Context, e.Message); Log?.AppendLine(string.Empty); return false; @@ -96,7 +94,7 @@ public bool Exec() { var errMsg = string.Join("\n", errs).Trim(); if (!string.IsNullOrEmpty(errMsg)) - Dispatcher.UIThread.Post(() => App.RaiseException(Context, errMsg)); + App.RaiseException(Context, errMsg); } return false; diff --git a/src/Commands/Discard.cs b/src/Commands/Discard.cs index f36ca6c98..6f076bb10 100644 --- a/src/Commands/Discard.cs +++ b/src/Commands/Discard.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.IO; -using Avalonia.Threading; - namespace SourceGit.Commands { public static class Discard @@ -36,10 +34,7 @@ public static void All(string repo, bool includeIgnored, Models.ICommandLog log) } catch (Exception e) { - Dispatcher.UIThread.Invoke(() => - { - App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}"); - }); + App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}"); } new Reset(repo, "HEAD", "--hard") { Log = log }.Exec(); @@ -78,10 +73,7 @@ public static void Changes(string repo, List changes, Models.ICom } catch (Exception e) { - Dispatcher.UIThread.Invoke(() => - { - App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}"); - }); + App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}"); } if (restores.Count > 0) diff --git a/src/Commands/GenerateCommitMessage.cs b/src/Commands/GenerateCommitMessage.cs index df61fdd29..7b486abd1 100644 --- a/src/Commands/GenerateCommitMessage.cs +++ b/src/Commands/GenerateCommitMessage.cs @@ -3,8 +3,6 @@ using System.Text; using System.Threading; -using Avalonia.Threading; - namespace SourceGit.Commands { /// @@ -86,7 +84,7 @@ public void Exec() } catch (Exception e) { - Dispatcher.UIThread.Post(() => App.RaiseException(_repo, $"Failed to generate commit message: {e}")); + App.RaiseException(_repo, $"Failed to generate commit message: {e}"); } } diff --git a/src/Commands/GitFlow.cs b/src/Commands/GitFlow.cs index 1d33fa3a2..aeb7b0a50 100644 --- a/src/Commands/GitFlow.cs +++ b/src/Commands/GitFlow.cs @@ -1,5 +1,4 @@ using System.Text; -using Avalonia.Threading; namespace SourceGit.Commands { @@ -43,7 +42,7 @@ public static bool Start(string repo, Models.GitFlowBranchType type, string name start.Args = $"flow hotfix start {name}"; break; default: - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!")); + App.RaiseException(repo, "Bad git-flow branch type!!!"); return false; } @@ -68,7 +67,7 @@ public static bool Finish(string repo, Models.GitFlowBranchType type, string nam builder.Append("hotfix"); break; default: - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, "Bad git-flow branch type!!!")); + App.RaiseException(repo, "Bad git-flow branch type!!!"); return false; } diff --git a/src/Commands/MergeTool.cs b/src/Commands/MergeTool.cs index fc6d0d75c..bdc948813 100644 --- a/src/Commands/MergeTool.cs +++ b/src/Commands/MergeTool.cs @@ -1,7 +1,5 @@ using System.IO; -using Avalonia.Threading; - namespace SourceGit.Commands { public static class MergeTool @@ -24,14 +22,14 @@ public static bool OpenForMerge(string repo, int toolType, string toolPath, stri if (!File.Exists(toolPath)) { - Dispatcher.UIThread.Post(() => App.RaiseException(repo, $"Can NOT find external merge tool in '{toolPath}'!")); + App.RaiseException(repo, $"Can NOT find external merge tool in '{toolPath}'!"); return false; } var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType); if (supported == null) { - Dispatcher.UIThread.Post(() => App.RaiseException(repo, "Invalid merge tool in preference setting!")); + App.RaiseException(repo, "Invalid merge tool in preference setting!"); return false; } @@ -54,14 +52,14 @@ public static bool OpenForDiff(string repo, int toolType, string toolPath, Model if (!File.Exists(toolPath)) { - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, $"Can NOT find external diff tool in '{toolPath}'!")); + App.RaiseException(repo, $"Can NOT find external diff tool in '{toolPath}'!"); return false; } var supported = Models.ExternalMerger.Supported.Find(x => x.Type == toolType); if (supported == null) { - Dispatcher.UIThread.Post(() => App.RaiseException(repo, "Invalid merge tool in preference setting!")); + App.RaiseException(repo, "Invalid merge tool in preference setting!"); return false; } diff --git a/src/Commands/QueryLocalChanges.cs b/src/Commands/QueryLocalChanges.cs index 788ed6172..bf331e93e 100644 --- a/src/Commands/QueryLocalChanges.cs +++ b/src/Commands/QueryLocalChanges.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Text.RegularExpressions; -using Avalonia.Threading; - namespace SourceGit.Commands { public partial class QueryLocalChanges : Command @@ -25,7 +23,7 @@ public QueryLocalChanges(string repo, bool includeUntracked = true) var rs = ReadToEnd(); if (!rs.IsSuccess) { - Dispatcher.UIThread.Post(() => App.RaiseException(Context, rs.StdErr)); + App.RaiseException(Context, rs.StdErr); return outs; } diff --git a/src/Commands/SaveChangesAsPatch.cs b/src/Commands/SaveChangesAsPatch.cs index b10037a1f..329cf823d 100644 --- a/src/Commands/SaveChangesAsPatch.cs +++ b/src/Commands/SaveChangesAsPatch.cs @@ -3,8 +3,6 @@ using System.Diagnostics; using System.IO; -using Avalonia.Threading; - namespace SourceGit.Commands { public static class SaveChangesAsPatch @@ -74,10 +72,7 @@ private static bool ProcessSingleChange(string repo, Models.DiffOption opt, File } catch (Exception e) { - Dispatcher.UIThread.Invoke(() => - { - App.RaiseException(repo, "Save change to patch failed: " + e.Message); - }); + App.RaiseException(repo, "Save change to patch failed: " + e.Message); return false; } } diff --git a/src/Commands/SaveRevisionFile.cs b/src/Commands/SaveRevisionFile.cs index b6127ea60..db0b99dcf 100644 --- a/src/Commands/SaveRevisionFile.cs +++ b/src/Commands/SaveRevisionFile.cs @@ -2,8 +2,6 @@ using System.Diagnostics; using System.IO; -using Avalonia.Threading; - namespace SourceGit.Commands { public static class SaveRevisionFile @@ -53,10 +51,7 @@ private static void ExecCmd(string repo, string args, string outputFile, Stream } catch (Exception e) { - Dispatcher.UIThread.Invoke(() => - { - App.RaiseException(repo, "Save file failed: " + e.Message); - }); + App.RaiseException(repo, "Save file failed: " + e.Message); } } } diff --git a/src/Commands/UnstageChangesForAmend.cs b/src/Commands/UnstageChangesForAmend.cs index 19def067a..06a718521 100644 --- a/src/Commands/UnstageChangesForAmend.cs +++ b/src/Commands/UnstageChangesForAmend.cs @@ -3,8 +3,6 @@ using System.Diagnostics; using System.Text; -using Avalonia.Threading; - namespace SourceGit.Commands { public class UnstageChangesForAmend @@ -75,16 +73,13 @@ public bool Exec() proc.Close(); if (!rs) - Dispatcher.UIThread.Invoke(() => App.RaiseException(_repo, err)); + App.RaiseException(_repo, err); return rs; } catch (Exception e) { - Dispatcher.UIThread.Invoke(() => - { - App.RaiseException(_repo, "Failed to unstage changes: " + e.Message); - }); + App.RaiseException(_repo, "Failed to unstage changes: " + e.Message); return false; } } diff --git a/src/ViewModels/ExecuteCustomAction.cs b/src/ViewModels/ExecuteCustomAction.cs index db3b14fd5..fa734ed5f 100644 --- a/src/ViewModels/ExecuteCustomAction.cs +++ b/src/ViewModels/ExecuteCustomAction.cs @@ -237,7 +237,7 @@ private void Run(string args) } catch (Exception e) { - CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message)); + App.RaiseException(_repo.FullPath, e.Message); } } @@ -284,12 +284,12 @@ private void RunAndWait(string args, Models.ICommandLog log) { var errMsg = builder.ToString().Trim(); if (!string.IsNullOrEmpty(errMsg)) - CallUIThread(() => App.RaiseException(_repo.FullPath, errMsg)); + App.RaiseException(_repo.FullPath, errMsg); } } catch (Exception e) { - CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message)); + App.RaiseException(_repo.FullPath, e.Message); } proc.Close(); diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index ede6d84cc..fcb34ceba 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -4,6 +4,7 @@ using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Input; +using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; @@ -394,6 +395,12 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page) public void DispatchNotification(string pageId, string message, bool isError) { + if (!Dispatcher.UIThread.CheckAccess()) + { + Dispatcher.UIThread.Invoke(() => DispatchNotification(pageId, message, isError)); + return; + } + var notification = new Models.Notification() { IsError = isError, diff --git a/src/ViewModels/RevisionCompare.cs b/src/ViewModels/RevisionCompare.cs index f04d1e301..0af66a309 100644 --- a/src/ViewModels/RevisionCompare.cs +++ b/src/ViewModels/RevisionCompare.cs @@ -124,7 +124,7 @@ public void SaveAsPatch(string saveTo) { var succ = Commands.SaveChangesAsPatch.ProcessRevisionCompareChanges(_repo, _changes, GetSHA(_startPoint), GetSHA(_endPoint), saveTo); if (succ) - Dispatcher.UIThread.Invoke(() => App.SendNotification(_repo, App.Text("SaveAsPatchSuccess"))); + App.SendNotification(_repo, App.Text("SaveAsPatchSuccess")); }); }