From 9e9f1cadf103f22d059cbff24d8da0e262257256 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 16 Aug 2017 12:32:54 -0400 Subject: [PATCH] Moving CrestronInvoke features into new ThreadingUtils class, supports multi-platform --- ICD.Common.Utils/CrestronUtils.cs | 35 ---------- .../ICD.Common.Utils_SimplSharp.csproj | 1 + ICD.Common.Utils/ThreadingUtils.cs | 64 +++++++++++++++++++ 3 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 ICD.Common.Utils/ThreadingUtils.cs diff --git a/ICD.Common.Utils/CrestronUtils.cs b/ICD.Common.Utils/CrestronUtils.cs index 308841f..64aed9d 100644 --- a/ICD.Common.Utils/CrestronUtils.cs +++ b/ICD.Common.Utils/CrestronUtils.cs @@ -187,41 +187,6 @@ namespace ICD.Common.Utils CrestronConsole.SendControlSystemCommand("reboot", ref consoleResult); } - /// - /// Runs CrestronInvoke but catches any unhandled exceptions to prevent the program from terminating. - /// http://www.crestronlabs.com/showthread.php?12205-Exception-in-CrestronInvoke-thread-crashes-the-program - /// - /// - [PublicAPI] - public static object SafeInvoke(Action callback) - { - return SafeInvoke(unused => callback(), null); - } - - /// - /// Runs CrestronInvoke but catches any unhandled exceptions to prevent the program from terminating. - /// http://www.crestronlabs.com/showthread.php?12205-Exception-in-CrestronInvoke-thread-crashes-the-program - /// - /// - /// - /// - [PublicAPI] - public static object SafeInvoke(Action callback, T param) - { - return CrestronInvoke.BeginInvoke(unused => - { - try - { - callback(param); - } - catch (Exception e) - { - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Error, e, e.Message); - } - }, null); - } - #endregion /// diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 5c10690..e2611c0 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -144,6 +144,7 @@ + diff --git a/ICD.Common.Utils/ThreadingUtils.cs b/ICD.Common.Utils/ThreadingUtils.cs new file mode 100644 index 0000000..d5d1f22 --- /dev/null +++ b/ICD.Common.Utils/ThreadingUtils.cs @@ -0,0 +1,64 @@ +using System; +#if SIMPLSHARP +using Crestron.SimplSharp; +#else +using System.Threading.Tasks; +#endif +using ICD.Common.Properties; +using ICD.Common.Services; +using ICD.Common.Services.Logging; + +namespace ICD.Common.Utils +{ + public static class ThreadingUtils + { + /// + /// Executes the callback as a short-lived, threaded task. + /// + /// + [PublicAPI] + public static object SafeInvoke(Action callback) + { + return SafeInvoke(unused => callback(), null); + } + + /// + /// Executes the callback as a short-lived, threaded task. + /// + /// + /// + /// + [PublicAPI] + public static object SafeInvoke(Action callback, T param) + { +#if SIMPLSHARP + return CrestronInvoke.BeginInvoke(unused => GetHandledCallback(callback, param), null); +#else + return Task.Run(() => GetHandledCallback(callback, param)); +#endif + } + + /// + /// Wraps the given callback in a try/catch to avoid crashing crestron programs. + /// http://www.crestronlabs.com/showthread.php?12205-Exception-in-CrestronInvoke-thread-crashes-the-program + /// + /// + /// + /// + private static Action GetHandledCallback(Action callback, T param) + { + return () => + { + try + { + callback(param); + } + catch (Exception e) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Error, e, e.Message); + } + }; + } + } +}