コード例 #1
0
        /// <summary>
        /// 设置操作类型,也即设置处理事件
        /// </summary>
        public void SetActionType(ParamModel paramModel)
        {
            //赋值为null
            this._result = null;

            //得到处理程序,若有异常直接抛出
            _process = ProcessFactory.GetProcessByMethod(this, paramModel);

            //开始执行
            _process?.Begin();
        }
コード例 #2
0
        string ProcessAndResult(string dataParam, ParamModel paramModel)
        {
            var resultModel = new ResultModel();

            resultModel.IsSuccess = true;
            resultModel.Result    = "666";
            try
            {
                //设置并执行相应操作,核心方法
                SetActionType(paramModel);

                try
                {
                    //得到执行结果
                    while (!CommonCla.IsTimeout(paramModel.StartTime, paramModel.Timeout))
                    {
                        resultModel.Result = _result;
                        if (resultModel.Result == null)
                        {
                            if (MonitorStopProcess(paramModel.StopKey))
                            {
                                resultModel.Result = ArtificialCode.A_RequestNormalBreak.ToString();
                                break;
                            }

                            if (IsDisposed)
                            {
                                throw new Exception(ArtificialCode.A_RequestAccidentBreak.ToString());
                            }
                            Thread.Sleep(100);
                        }
                        else
                        {
                            resultModel.IsSuccess = true;
                            break;
                        }
                    }

                    if (resultModel.Result == null)
                    {
                        throw new Exception(ArtificialCode.A_TimeOutResult.ToString());
                    }
                }
                catch (Exception ex)//获取结果发生错误
                {
                    resultModel.Result = ex.Message;
                    //_log.ErrorFormat("耗时:{0}\r\n{1}\r\n{2}", CommonCla.GetMilliseconds(paramModel.StartTime), dataParam, resultModel.Result);
                }
            }
            catch (Exception ex)//解析参数发生错误
            {
                //如果Start发生异常
                _isWorking = false;

                resultModel.Result = ex.Message;
                //_log.FatalFormat("{0}\r\nStart()\r\n{1}", paramModel.Method, ex.Message);
            }
            finally
            {
                Task.Run(() =>
                {
                    //本次执行完成,退出使用  //不阻塞执行,尽快返回结果
                    try
                    {
                        //TODO这个地方有空引用,正常这是不可能的,暂时不处理
                        _process?.End();
                        _process = null;
                    }
                    catch (Exception ee)
                    {
                        //var position = nameof(ProcessForm) + "--" + nameof(Quit);
                        System.Diagnostics.Process.GetCurrentProcess().Kill();
                    }
                });
            }

            return(JsonConvert.SerializeObject(resultModel));
        }