Пример #1
0
        private async Task <MethodInvocationResult> ExecuteMethodInvocation(MethodInvocation methodInvocation)
        {
            object returnValue = null;
            string exception;
            var    success       = false;
            var    nameConverter = repository.NameConverter;

            //make sure we don't throw exceptions in the executor task
            try
            {
                var result = await repository.TryCallMethodAsync(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray()).ConfigureAwait(false);

                success     = result.Success;
                returnValue = result.ReturnValue;
                exception   = result.Exception;
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            return(new MethodInvocationResult
            {
                BrowserId = methodInvocation.BrowserId,
                CallbackId = methodInvocation.CallbackId,
                FrameId = methodInvocation.FrameId,
                Message = exception,
                Result = returnValue,
                Success = success,
                NameConverter = nameConverter
            });
        }
        private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)
        {
            object result = null;
            string exception;
            var    success = false;

            //make sure we don't throw exceptions in the executor task
            try
            {
                success = repository.TryCallMethod(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray(), out result, out exception);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            return(new MethodInvocationResult
            {
                BrowserId = methodInvocation.BrowserId,
                CallbackId = methodInvocation.CallbackId,
                FrameId = methodInvocation.FrameId,
                Message = exception,
                Result = result,
                Success = success
            });
        }
Пример #3
0
        public void Enqueue(MethodInvocation methodInvocation)
        {
            methodRunnerQueueTaskFactory.StartNew(() =>
            {
                var result = ExecuteMethodInvocation(methodInvocation);

                var handler = MethodInvocationComplete;
                if (!cancellationTokenSource.Token.IsCancellationRequested && handler != null)
                {
                    handler(this, new MethodInvocationCompleteArgs(result));
                }
            }, cancellationTokenSource.Token);
        }
Пример #4
0
        public void Enqueue(MethodInvocation methodInvocation)
        {
            Task.Factory.StartNew(() =>
            {
                var result = ExecuteMethodInvocation(methodInvocation);

                var handler = MethodInvocationComplete;
                if (!cancellationTokenSource.Token.IsCancellationRequested && handler != null)
                {
                    handler(this, new MethodInvocationCompleteArgs(result));
                }
            }, cancellationTokenSource.Token, TaskCreationOptions.HideScheduler, taskScheduler);
        }
Пример #5
0
        private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)
        {
            object returnValue = null;
            string exception;
            var    success       = false;
            var    nameConverter = repository.NameConverter;

            //make sure we don't throw exceptions in the executor task
            try
            {
                var result = repository.TryCallMethod(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray());
                success     = result.Success;
                returnValue = result.ReturnValue;
                exception   = result.Exception;

                //We don't support Tasks by default
                if (success && returnValue != null && (typeof(Task).IsAssignableFrom(returnValue.GetType())))
                {
                    //Use StringBuilder to improve the formatting/readability of the error message
                    //I'm sure there's a better way I just cannot remember of the top of my head so going
                    //with this for now, as it's only for error scenaiors I'm not concerned about performance.
                    var builder = new System.Text.StringBuilder();
                    builder.AppendLine("Your method returned a Task which is not supported by default you must set CefSharpSettings.ConcurrentTaskExecution = true; before creating your first ChromiumWebBrowser instance.");
                    builder.AppendLine("This will likely change to the default at some point in the near future, subscribe to the issue link below to be notified of any changes.");
                    builder.AppendLine("See https://github.com/cefsharp/CefSharp/issues/2758 for more details, please report any issues you have there, make sure you have an example ready that reproduces your problem.");

                    success   = false;
                    result    = null;
                    exception = builder.ToString();
                }
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            return(new MethodInvocationResult
            {
                BrowserId = methodInvocation.BrowserId,
                CallbackId = methodInvocation.CallbackId,
                FrameId = methodInvocation.FrameId,
                Message = exception,
                Result = returnValue,
                Success = success,
                NameConverter = nameConverter
            });
        }
        public void Enqueue(MethodInvocation methodInvocation)
        {
            var task = new Task(() =>
            {
                var result = ExecuteMethodInvocation(methodInvocation);

                //If the call failed or returned null then we'll fire the event immediately
                if (!result.Success || result.Result == null)
                {
                    OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                }
                else
                {
                    var resultType = result.Result.GetType();

                    //If the returned type is Task then we'll ContinueWith and perform the processing then.
                    if (typeof(Task).IsAssignableFrom(resultType))
                    {
                        var resultTask = (Task)result.Result;

                        if (resultType.IsGenericType)
                        {
                            resultTask.ContinueWith((t) =>
                            {
                                if (t.Status == TaskStatus.RanToCompletion)
                                {
                                    //We use some reflection to get the Result
                                    //If someone has a better way of doing this then please submit a PR
                                    result.Result = resultType.GetProperty("Result").GetValue(resultTask, null);
                                }
                                else
                                {
                                    result.Success         = false;
                                    result.Result          = null;
                                    var aggregateException = t.Exception;
                                    //TODO: Add support for passing a more complex message
                                    // to better represent the Exception
                                    if (aggregateException.InnerExceptions.Count == 1)
                                    {
                                        result.Message = aggregateException.InnerExceptions[0].ToString();
                                    }
                                    else
                                    {
                                        result.Message = t.Exception.ToString();
                                    }
                                }

                                OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                            },
                                                    cancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler.Default);
                        }
                        else
                        {
                            //If it's not a generic Task then it doesn't have a return object
                            //So we'll just set the result to null and continue on
                            result.Result = null;

                            OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                        }
                    }
                    else
                    {
                        OnMethodInvocationComplete(result, cancellationTokenSource.Token);
                    }
                }
            }, cancellationTokenSource.Token);

            task.Start(TaskScheduler.Default);
        }
Пример #7
0
        public void Enqueue(MethodInvocation methodInvocation)
        {
            var task = new Task <MethodInvocationResult>(() => ExecuteMethodInvocation(methodInvocation));

            queue.Add(task);
        }
Пример #8
0
 public void Enqueue(MethodInvocation methodInvocation)
 {
     queue.Add(() => ExecuteMethodInvocation(methodInvocation));
 }
Пример #9
0
 public void Enqueue(MethodInvocation methodInvocation)
 {
     var task = new Task<MethodInvocationResult>(() => ExecuteMethodInvocation(methodInvocation));
     queue.Add(task);
 }
Пример #10
0
        private MethodInvocationResult ExecuteMethodInvocation(MethodInvocation methodInvocation)
        {
            object result = null;
            string exception;
            var success = false;

            //make sure we don't throw exceptions in the executor task
            try
            {
                success = repository.TryCallMethod(methodInvocation.ObjectId, methodInvocation.MethodName, methodInvocation.Parameters.ToArray(), out result, out exception);
            }
            catch (Exception e)
            {
                exception = e.Message;
            }

            return new MethodInvocationResult
            {
                BrowserId = methodInvocation.BrowserId,
                CallbackId = methodInvocation.CallbackId,
                FrameId = methodInvocation.FrameId,
                Message = exception,
                Result = result,
                Success = success
            };
        }