/// <summary> /// 命令是不是cmd类型 /// </summary> /// <param name="cmd"></param> /// <returns></returns> internal bool IsType(CmdString cmd) { if (_cmdString.StartsWith(cmd._cmdString, StringComparison.OrdinalIgnoreCase)) { return(true); } return(false); }
/// <summary> ///命令字符串是否相匹配 /// </summary> internal bool Match(CmdString cmd) { if (_cmdString.Equals(cmd._cmdString, StringComparison.OrdinalIgnoreCase)) { return(true); } return(false); }
//此处封装了关键的方法:Start,Stop,Continue,Pause internal void Start(List <MirrorBlockInfo> mirrorBlockInfos, int startedFileIndex = 0, long startedPos = 0) { if (mirrorBlockInfos.Count < 1) { _stateReporter.Report(CmdStrings.NoSelectedPartition); return; } _curState = null; //设置反馈,以及执行命令 if (_mirrorBackgroundProcess != null) { _mirrorBackgroundProcess.CallBack -= OnCallBack; _mirrorBackgroundProcess.Close(); } _mirrorBackgroundProcess = new MirrorBackgroundProcess(); _mirrorBackgroundProcess.CallBack += OnCallBack; bool isInitialized = _mirrorBackgroundProcess.Initialize(); if (!isInitialized) { return; } for (int i = startedFileIndex; i < mirrorBlockInfos.Count; i++) { var item = mirrorBlockInfos[i]; //以下是构建参数 string arg = string.Format(@"{0}|{1}|{2}|{3}|{4}|{5}", CmdStrings.StartMirror, _deviceID, _isHtc, item.TargetMirrorFile, item.SourceBlockPath, startedPos); CmdString startCmd = new CmdString(arg); _mirrorBackgroundProcess.ExcuteCmd(startCmd); _pauseInfo.SetPauseFileInfo(i, mirrorBlockInfos); while (true) { if (_curState != null) { if (_curState.Match(CmdStrings.FinishState)) { break; } else if (_curState.Match(CmdStrings.StopMirror) || _curState.Match(CmdStrings.Exception)) { return; } } Thread.Sleep(1000); } } _mirrorBackgroundProcess.CallBack -= OnCallBack; _mirrorBackgroundProcess.Close(); _stateReporter.Report(CmdStrings.AllFinishState); }
private void OnCallBack(string info) { CmdString cmd = CmdStrings.UnknowException; if (info != null) { cmd = new CmdString(info); } _curState = cmd; _stateReporter.Report(cmd); }
/// <summary> /// 执行命令 /// </summary> /// <param name="cmd"></param> public void ExcuteCmd(CmdString cmd) { if (_isInitialized) { if (_curProcess.HasExited) { OnCallBack(string.Format("{0}|Background Application Has Stopped!", CmdStrings.StopMirror)); } else { _curProcess.StandardInput.WriteLine(cmd.ToString()); } } }
/// <summary> /// 状态变化事件 /// </summary> private void StateChanged(CmdString state) { if (state.IsType(CmdStrings.Progress)) { CmdString progress = state.GetChildCmd(); long finisedSize = 0; if (long.TryParse(progress.ToString(), out finisedSize)) { ProgressChanged(finisedSize); _pauseInfo.SetPausePos(finisedSize); } } else if (state.Match(CmdStrings.StartMirror)) { SourcePosition.IsMirroring = true; } else if (state.Match(CmdStrings.AllFinishState)) { _msgBox.ShowDialogSuccessMsg("镜像完成"); SourcePosition.IsMirroring = false; ProgressPosition.FinishedSize = ProgressPosition.TotalSize; ProgressPosition.Stop(); } else if (state.IsType(CmdStrings.Exception)) { _msgBox.ShowErrorMsg("镜像失败" + state.GetChildCmd()); SourcePosition.IsMirroring = false; } else if (state.Match(CmdStrings.StopMirror)) { _msgBox.ShowDialogSuccessMsg("镜像停止"); SourcePosition.IsMirroring = false; } else if (state.Match(CmdStrings.NoSelectedPartition)) { _msgBox.ShowDialogSuccessMsg("请选择至少一个分区"); SourcePosition.IsMirroring = false; } else if (state.Match(CmdStrings.PauseMirror) || state.Match(CmdStrings.ContinueMirror)) { _msgBox.ShowDialogSuccessMsg(state.ToString()); } }