Skip to content

Commit

Permalink
fixed ShareX#6317: await OCRImage inside HandleHotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaex committed Jul 1, 2022
1 parent f7f6870 commit 101c4e1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
4 changes: 2 additions & 2 deletions ShareX/Forms/ActionsToolbarForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ private void UpdateToolbar(List<HotkeyType> actions)
Image = TaskHelpers.FindMenuIcon(action)
};

tsb.Click += (sender, e) =>
tsb.Click += async (sender, e) =>
{
if (Program.Settings.ActionsToolbarStayTopMost)
{
TopMost = false;
}

TaskHelpers.ExecuteJob(action);
await TaskHelpers.ExecuteJob(action);

if (Program.Settings.ActionsToolbarStayTopMost)
{
Expand Down
22 changes: 11 additions & 11 deletions ShareX/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public MainForm()
InitializeControls();
}

private void MainForm_HandleCreated(object sender, EventArgs e)
private async void MainForm_HandleCreated(object sender, EventArgs e)
{
RunPuushTasks();

Expand All @@ -63,7 +63,7 @@ private void MainForm_HandleCreated(object sender, EventArgs e)

DebugHelper.WriteLine("Startup time: {0} ms", Program.StartTimer.ElapsedMilliseconds);

Program.CLI.UseCommandLineArgs();
await Program.CLI.UseCommandLineArgs();

if (Program.Settings.ActionsToolbarRunAtStartup)
{
Expand Down Expand Up @@ -350,11 +350,11 @@ private void InitHotkeys()
});
}

private void HandleHotkeys(HotkeySettings hotkeySetting)
private async void HandleHotkeys(HotkeySettings hotkeySetting)
{
DebugHelper.WriteLine("Hotkey triggered. " + hotkeySetting);

TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);
await TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);
}

private void UpdateWorkflowsMenu()
Expand Down Expand Up @@ -436,7 +436,7 @@ private ToolStripMenuItem WorkflowMenuItem(HotkeySettings hotkeySetting)
{
tsmi.Font = new Font(tsmi.Font, FontStyle.Bold);
}
tsmi.Click += (sender, e) => TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);
tsmi.Click += async (sender, e) => await TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);
return tsmi;
}

Expand Down Expand Up @@ -1947,13 +1947,13 @@ private void tsbAbout_Click(object sender, EventArgs e)

#region Tray events

private void niTray_MouseUp(object sender, MouseEventArgs e)
private async void niTray_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Program.Settings.TrayLeftDoubleClickAction == HotkeyType.None)
{
TaskHelpers.ExecuteJob(Program.Settings.TrayLeftClickAction);
await TaskHelpers.ExecuteJob(Program.Settings.TrayLeftClickAction);
}
else
{
Expand All @@ -1969,24 +1969,24 @@ private void niTray_MouseUp(object sender, MouseEventArgs e)
trayClickCount = 0;
timerTraySingleClick.Stop();

TaskHelpers.ExecuteJob(Program.Settings.TrayLeftDoubleClickAction);
await TaskHelpers.ExecuteJob(Program.Settings.TrayLeftDoubleClickAction);
}
}
}
else if (e.Button == MouseButtons.Middle)
{
TaskHelpers.ExecuteJob(Program.Settings.TrayMiddleClickAction);
await TaskHelpers.ExecuteJob(Program.Settings.TrayMiddleClickAction);
}
}

private void timerTraySingleClick_Tick(object sender, EventArgs e)
private async void timerTraySingleClick_Tick(object sender, EventArgs e)
{
if (trayClickCount == 1)
{
trayClickCount = 0;
timerTraySingleClick.Stop();

TaskHelpers.ExecuteJob(Program.Settings.TrayLeftClickAction);
await TaskHelpers.ExecuteJob(Program.Settings.TrayLeftClickAction);
}
}

Expand Down
10 changes: 7 additions & 3 deletions ShareX/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ You should have received a copy of the GNU General Public License
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

#if MicrosoftStore
Expand Down Expand Up @@ -394,7 +395,10 @@ private static void SingleInstanceCallback(object sender, InstanceCallbackEventA
{
if (WaitFormLoad(5000))
{
MainForm.InvokeSafe(() => UseCommandLineArgs(args.CommandLineArgs));
MainForm.InvokeSafe(async () =>
{
await UseCommandLineArgs(args.CommandLineArgs);
});
}
}

Expand All @@ -412,7 +416,7 @@ private static bool WaitFormLoad(int wait)
return false;
}

private static void UseCommandLineArgs(string[] args)
private static async Task UseCommandLineArgs(string[] args)
{
if (args == null || args.Length < 1)
{
Expand All @@ -433,7 +437,7 @@ private static void UseCommandLineArgs(string[] args)
CLIManager cli = new CLIManager(args);
cli.ParseCommands();

CLI.UseCommandLineArgs(cli.Commands);
await CLI.UseCommandLineArgs(cli.Commands);
}

private static void UpdatePersonalPath()
Expand Down
19 changes: 11 additions & 8 deletions ShareX/ShareXCLIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You should have received a copy of the GNU General Public License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ShareX
{
Expand All @@ -36,12 +37,12 @@ public ShareXCLIManager(string[] arguments) : base(arguments)
{
}

public void UseCommandLineArgs()
public async Task UseCommandLineArgs()
{
UseCommandLineArgs(Commands);
await UseCommandLineArgs(Commands);
}

public void UseCommandLineArgs(List<CLICommand> commands)
public async Task UseCommandLineArgs(List<CLICommand> commands)
{
TaskSettings taskSettings = FindCLITask(commands);

Expand All @@ -51,7 +52,7 @@ public void UseCommandLineArgs(List<CLICommand> commands)

if (command.IsCommand)
{
if (CheckCustomUploader(command) || CheckImageEffect(command) || CheckCLIHotkey(command) || CheckCLIWorkflow(command) ||
if (CheckCustomUploader(command) || CheckImageEffect(command) || await CheckCLIHotkey(command) || await CheckCLIWorkflow(command) ||
CheckNativeMessagingInput(command))
{
}
Expand Down Expand Up @@ -121,21 +122,22 @@ private bool CheckImageEffect(CLICommand command)
return false;
}

private bool CheckCLIHotkey(CLICommand command)
private async Task<bool> CheckCLIHotkey(CLICommand command)
{
foreach (HotkeyType job in Helpers.GetEnums<HotkeyType>())
{
if (command.CheckCommand(job.ToString()))
{
TaskHelpers.ExecuteJob(job, command);
await TaskHelpers.ExecuteJob(job, command);

return true;
}
}

return false;
}

private bool CheckCLIWorkflow(CLICommand command)
private async Task<bool> CheckCLIWorkflow(CLICommand command)
{
if (Program.HotkeysConfig != null && command.CheckCommand("workflow") && !string.IsNullOrEmpty(command.Parameter))
{
Expand All @@ -145,7 +147,8 @@ private bool CheckCLIWorkflow(CLICommand command)
{
if (command.Parameter == hotkeySetting.TaskSettings.ToString())
{
TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);
await TaskHelpers.ExecuteJob(hotkeySetting.TaskSettings);

return true;
}
}
Expand Down
12 changes: 6 additions & 6 deletions ShareX/TaskHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ namespace ShareX
{
public static class TaskHelpers
{
public static void ExecuteJob(HotkeyType job, CLICommand command = null)
public static async Task ExecuteJob(HotkeyType job, CLICommand command = null)
{
ExecuteJob(Program.DefaultTaskSettings, job, command);
await ExecuteJob(Program.DefaultTaskSettings, job, command);
}

public static void ExecuteJob(TaskSettings taskSettings)
public static async Task ExecuteJob(TaskSettings taskSettings)
{
ExecuteJob(taskSettings, taskSettings.Job);
await ExecuteJob(taskSettings, taskSettings.Job);
}

public static void ExecuteJob(TaskSettings taskSettings, HotkeyType job, CLICommand command = null)
public static async Task ExecuteJob(TaskSettings taskSettings, HotkeyType job, CLICommand command = null)
{
if (job == HotkeyType.None) return;

Expand Down Expand Up @@ -224,7 +224,7 @@ public static void ExecuteJob(TaskSettings taskSettings, HotkeyType job, CLIComm
OpenVideoThumbnailer(safeTaskSettings);
break;
case HotkeyType.OCR:
OCRImage(safeTaskSettings).GetAwaiter().GetResult();
await OCRImage(safeTaskSettings);
break;
case HotkeyType.QRCode:
OpenQRCode();
Expand Down

0 comments on commit 101c4e1

Please sign in to comment.