Moving CrestronInvoke features into new ThreadingUtils class, supports multi-platform

This commit is contained in:
Chris Cameron
2017-08-16 12:32:54 -04:00
parent 599ab2997f
commit 9e9f1cadf1
3 changed files with 65 additions and 35 deletions

View File

@@ -187,41 +187,6 @@ namespace ICD.Common.Utils
CrestronConsole.SendControlSystemCommand("reboot", ref consoleResult);
}
/// <summary>
/// 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
/// </summary>
/// <param name="callback"></param>
[PublicAPI]
public static object SafeInvoke(Action callback)
{
return SafeInvoke<object>(unused => callback(), null);
}
/// <summary>
/// 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
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="callback"></param>
/// <param name="param"></param>
[PublicAPI]
public static object SafeInvoke<T>(Action<T> callback, T param)
{
return CrestronInvoke.BeginInvoke(unused =>
{
try
{
callback(param);
}
catch (Exception e)
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Error, e, e.Message);
}
}, null);
}
#endregion
/// <summary>

View File

@@ -144,6 +144,7 @@
<Compile Include="SafeMutex.cs" />
<Compile Include="StringUtils.cs" />
<Compile Include="TableBuilder.cs" />
<Compile Include="ThreadingUtils.cs" />
<Compile Include="Timers\IcdStopwatch.cs" />
<Compile Include="Timers\IcdTimer.cs" />
<Compile Include="Timers\Repeater.cs" />

View File

@@ -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
{
/// <summary>
/// Executes the callback as a short-lived, threaded task.
/// </summary>
/// <param name="callback"></param>
[PublicAPI]
public static object SafeInvoke(Action callback)
{
return SafeInvoke<object>(unused => callback(), null);
}
/// <summary>
/// Executes the callback as a short-lived, threaded task.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="callback"></param>
/// <param name="param"></param>
[PublicAPI]
public static object SafeInvoke<T>(Action<T> callback, T param)
{
#if SIMPLSHARP
return CrestronInvoke.BeginInvoke(unused => GetHandledCallback(callback, param), null);
#else
return Task.Run(() => GetHandledCallback(callback, param));
#endif
}
/// <summary>
/// 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
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="callback"></param>
/// <param name="param"></param>
private static Action GetHandledCallback<T>(Action<T> callback, T param)
{
return () =>
{
try
{
callback(param);
}
catch (Exception e)
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Error, e, e.Message);
}
};
}
}
}