mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 03:05:06 +00:00
Moving CrestronInvoke features into new ThreadingUtils class, supports multi-platform
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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" />
|
||||
|
||||
64
ICD.Common.Utils/ThreadingUtils.cs
Normal file
64
ICD.Common.Utils/ThreadingUtils.cs
Normal 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user