/// <summary> /// Invokes the specified action using this toolkit. /// </summary> /// <param name="a">The action to invoke in the context of this toolkit.</param> /// <remarks> /// Invoke allows dynamic toolkit switching. It will set <see cref="CurrentEngine"/> to this toolkit and reset /// it back to its original value after the action has been executed. /// /// Invoke must be executed on the UI thread. The action will not be synchronized with the main UI thread automatically. /// </remarks> /// <returns><c>true</c> if the action has been executed sucessfully; otherwise, <c>false</c>.</returns> public bool Invoke(Action a) { ToolkitContext currentContext = SwitchContext(); try { EnterUserCode(); a(); ExitUserCode(null); return(true); } catch (Exception ex) { ExitUserCode(ex); return(false); } finally { currentContext.Restore(); } }
public T Invoke <T> (Func <T> func) { ToolkitContext currentContext = SwitchContext(); try { EnterUserCode(); var res = func(); ExitUserCode(null); return(res); } catch (Exception ex) { ExitUserCode(ex); return(default(T)); } finally { currentContext.Restore(); } }
/// <summary> /// Switches the current context to the context of this toolkit /// </summary> ToolkitContext SwitchContext() { var current = System.Threading.SynchronizationContext.Current; // Store the current engine and the current context (which is not necessarily the context of the engine) var currentContext = new ToolkitContext { SynchronizationContext = current, Engine = currentEngine }; currentEngine = this; if ((current as XwtSynchronizationContext)?.TargetToolkit != this) { System.Threading.SynchronizationContext.SetSynchronizationContext(SynchronizationContext); } return(currentContext); }
public HardDisksController(ToolkitContext context, IHostingEnvironment hostingEnvironment, ILogger <HardDisksController> logger) { _context = context; _hostingEnvironment = hostingEnvironment; _logger = logger; }