Пример #1
0
        private void SetState(TaskStateType state, Double?currentPrice)
        {
            this.State = state;

            if (TaskStateChange != null)
            {
                TaskStateChangeEventArgs args = new TaskStateChangeEventArgs(state, currentPrice);
                TaskStateChange.Invoke(this, args);
            }
        }
Пример #2
0
        private void NotifyExecutionHandler(TaskStateChangeEventArgs e)
        {
            if (e.HandlerType == null)
            {
                Logger.Info("没有回调,直接退出");
                return;
            }

            if (Enum.TryParse(e.StateName, out TaskStateEnum tmpState) == false)
            {
                return;
            }

            Task.Factory.StartNew(async() =>
            {
                var service = _serviceProvider.GetService(e.HandlerType);

                var modelType = e.HandlerType.GetInterfaces().FirstOrDefault(x => x.Name.Contains(nameof(ITaskHandler))).GetGenericArguments()[0];

                MethodInfo method = null;

                Task result = null;

                switch (tmpState)
                {
                case TaskStateEnum.Succeeded:
                    method = e.HandlerType.GetMethod(nameof(ITaskHandler.DoSuccess));
                    result = (Task)method.Invoke(service, new object[] { Newtonsoft.Json.JsonConvert.DeserializeObject(e.CallbackJson, modelType) });
                    await result;

                    if (TaskFinished(e.Id))
                    {
                        var methodFinished = e.HandlerType.GetMethod(nameof(ITaskHandler.DoFinished));
                        result             = (Task)methodFinished.Invoke(service, new object[] { Newtonsoft.Json.JsonConvert.DeserializeObject(e.CallbackJson, modelType) });
                        await result;
                    }
                    break;

                case TaskStateEnum.Failed:

                    var methods = e.HandlerType.GetMethods();

                    foreach (var m in methods)
                    {
                        if (m.Name.Equals(nameof(ITaskHandler.DoFailed)))
                        {
                            switch (m.GetParameters().Length)
                            {
                            case 1:
                                result = (Task)m.Invoke(service, new object[] { Newtonsoft.Json.JsonConvert.DeserializeObject(e.CallbackJson, modelType) });
                                await result;
                                break;

                            case 2:
                                result = (Task)m.Invoke(service, new object[] { Newtonsoft.Json.JsonConvert.DeserializeObject(e.CallbackJson, modelType), e.Error });
                                await result;
                                break;
                            }
                        }
                    }
                    break;

                default:
                    return;
                }
            });
        }