//--------------------------------------------------------------------------------------- // This code follows the architecture drawn by DotNetBrowser // (cf https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000109868-calling-javascript-from-net) // For an example of implementation, go to INTERNAL_InteropImplementation.cs and // ExecuteJavaScript_SimulatorImplementation method, in the first "if". //--------------------------------------------------------------------------------------- #if OPENSILVER private static object OnCallbackFromJavaScript <T>( int callbackId, string idWhereCallbackArgsAreStored, T callbackArgsObject, Func <int, int, string, T, Type[], object[]> makeArguments, bool isInSimulator, bool returnValue) { object result = null; var actionExecuted = false; Action action = () => { INTERNAL_SimulatorExecuteJavaScript.RunActionThenExecutePendingAsyncJSCodeExecutedDuringThatAction( () => { //-------------------- // Call the callback: //-------------------- try { result = CallMethod(callbackId, idWhereCallbackArgsAreStored, makeArguments, callbackArgsObject); actionExecuted = true; } catch (Exception ex) { #if DEBUG Console.Error.WriteLine("DEBUG: OnCallBack: OnCallBackFromJavascript: " + ex); #endif throw; } }); }; if (isInSimulator) { // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread: if (returnValue) { var timeout = TimeSpan.FromSeconds(30); INTERNAL_Simulator.WebControlDispatcherInvoke(action, timeout); if (!actionExecuted) { GenerateDeadlockException(timeout); } } else { INTERNAL_Simulator.WebControlDispatcherBeginInvoke(action); } } else { action(); } return(returnValue ? result : null); }
public object OnCallbackFromJavaScript( int callbackId, string idWhereCallbackArgsAreStored, JSArray callbackArgsObject, bool returnValue) { object result = null; var actionExecuted = false; Action action = () => { //-------------------- // Call the callback: //-------------------- try { result = CallMethod(callbackId, idWhereCallbackArgsAreStored, MakeArgumentsForCallback, callbackArgsObject); actionExecuted = true; } catch (global::System.Reflection.TargetInvocationException ex) { if (global::System.Diagnostics.Debugger.IsAttached) { if (ex.InnerException != null) { // We rethrow the inner exception if any, that is, the exception that happens in the C# code being called by "DynamicInvoke". cf. https://stackoverflow.com/questions/57383/in-c-how-can-i-rethrow-innerexception-without-losing-stack-trace INTERNAL_Simulator.SimulatorProxy.ThrowExceptionWithoutLosingStackTrace(ex.InnerException); } else { throw; } } else { INTERNAL_Simulator.SimulatorProxy.ShowException(ex.InnerException ?? ex); } } }; if (Interop.IsRunningInTheSimulator) { // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread: if (returnValue) { var timeout = TimeSpan.FromSeconds(30); INTERNAL_Simulator.WebControlDispatcherInvoke(action, timeout); if (!actionExecuted) { GenerateDeadlockException(timeout); } } else { INTERNAL_Simulator.WebControlDispatcherBeginInvoke(action); } } else { action(); } return(result); }