//sample that shows how to perform Live INstance Management //in this example, we want to migrate all active instances of a workflow to the latest version //Note: take due care when migrating active process instances since not all migration scenarios are supported public void LiveInstanceManagementSample() { //establish the connection WorkflowManagementServer K2Mgmt = new WorkflowManagementServer(); K2Mgmt.CreateConnection(); K2Mgmt.Connection.Open("connectionstring"); //get the proc set ID of the workflow so that we can get the latest version of the proc set SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter filter = new SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter(); Processes procs = null; filter.AddRegularFilter(ProcessFields.ProcessFullName, Comparison.Equals, "processFullName"); procs = K2Mgmt.GetProcesses(filter); int procSetId = procs[0].ProcSetID; //now get the latest version of the procset int latestVersion = 0; int latestVersionId = 0; Processes procVersions = K2Mgmt.GetProcessVersions(procSetId); foreach (Process procVersion in procVersions) { if (procVersion.VersionNumber > latestVersion) { latestVersion = procVersion.VersionNumber; latestVersionId = procVersion.ProcID; } } //now that we have the latest version of the workflow, //get the ID's of all the process instances of the workflow ProcessInstances procInstances = K2Mgmt.GetProcessInstancesAll("processFullName", "", ""); //leaving parameters blank effectively ignores that parameter foreach (ProcessInstance procInst in procInstances) { //no need to migrate ones that are already on this version if (procInst.ExecutingProcID != latestVersionId) { //must stop a non-stopped instance before attempting migration if (procInst.Status != "Stopped") { K2Mgmt.StopProcessInstances(procInst.ID); } //now migrate the instance to the latest version K2Mgmt.SetProcessInstanceVersion(procInst.ID, latestVersion); //restart the process K2Mgmt.StartProcessInstances(procInst.ID); } } //close the connection K2Mgmt.Connection.Close(); }
/// <summary> /// 结束流程 /// </summary> /// <param name="wfId"></param> /// <returns></returns> public static bool StopWorkflow(int wfId) { WorkflowManagementServer svr = new WorkflowManagementServer(K2ServerName, uint.Parse(K2ServerPort)); try { svr.Open(); bool flag = svr.StopProcessInstances(wfId); return(flag); } catch (Exception ex) { return(false); } finally { svr.Connection.Close(); } }
public static void SetProcessInstanceVersion() { WorkflowManagementServer wms = new WorkflowManagementServer(); wms.Open(GetK2ManagementServerConnectionString()); // wms.StopProcessInstances SqlHelper sqlHelper = new SqlHelper(K2ConnectionString); var dt = sqlHelper.ExecuteDataTable("SELECT ID FROM Server.ProcInst WHERE ProcID IN (1202,1217,1195,1176)"); if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { wms.StopProcessInstances(Convert.ToInt32(dr["ID"])); wms.SetProcessInstanceVersion(Convert.ToInt32(dr["ID"]), 68); wms.StartProcessInstances(Convert.ToInt32(dr["ID"])); } } }
//sample that shows how to manage process instances //in this sample, we want to stop all instances of a particular workflow public void ManageProcessInstances() { //establish the connection WorkflowManagementServer K2Mgmt = new WorkflowManagementServer(); K2Mgmt.CreateConnection(); K2Mgmt.Connection.Open("connectionstring"); //first, get the ID's of all the process instances of the workflow ProcessInstances procInstances = K2Mgmt.GetProcessInstancesAll("workflowfullname", "", ""); //leaving parameters blank effectively ignores that parameter foreach (ProcessInstance procInst in procInstances) { if (procInst.Status == "Active") { K2Mgmt.StopProcessInstances(procInst.ID); } } //close the connection K2Mgmt.Connection.Close(); }
new string[] { "ProcessInstanceId", "NewVersion", "ActivityName", "ResultStatus", "ResultMessage" })] // , "IsDefaultVersion", "VersionDate", "VersionDescription", "VersionLabel", "VersionNumber" public LIM.ProcessInstance MigrateProcessInstance() { List<LIM.ProcessInstance> results = new List<ProcessInstance>(); WorkflowManagementServer svr = new WorkflowManagementServer("localhost", 5555); try { svr.Open(); svr.StopProcessInstances(this.ProcessInstanceId); svr.SetProcessInstanceVersion(this.ProcessInstanceId, this.NewVersion); if (!string.IsNullOrWhiteSpace(this.ActivityName)) { svr.GotoActivity(this.ProcessInstanceId, ActivityName); } else { svr.StartProcessInstances(this.ProcessInstanceId); } } catch (Exception ex) { this.ResultStatus = "Exception"; this.ResultMessage = ex.GetBaseException().Message; return this; } finally { try { svr.Connection.Close(); svr.Connection.Dispose(); } catch { } svr = null; } this.ResultStatus = "Success"; return this; }
void Stop() { int procInstID = Convert.ToInt32(lblId.Text); bool isExist = ExistprocInst(procInstID); //1-停止K2流程 WorkflowManagementServer svr = new WorkflowManagementServer(); svr.CreateConnection(); svr.Connection.Open(WorkflowHelper.GetConnString4Management()); if (isExist) { try { bool flag = svr.StopProcessInstances(procInstID); } catch (Exception ex) { Logger.Write(this.GetType(), EnumLogLevel.Info, "*****StopProcessInstances:" + ex.Message); } } svr.Connection.Close(); bool isExistDataField = ExistDataField(procInstID); //2-更新K2的ispass=2; if (isExistDataField) { try { NameValueCollection dataFields = new NameValueCollection(); dataFields.Add("IsPass", "2"); WorkflowHelper.UpdateDataFields(procInstID.ToString(), dataFields, "founder\\zybpmadmin"); } catch (Exception ex) { Logger.Write(this.GetType(), EnumLogLevel.Info, "*****UpdateDataFields:" + ex.Message); } } //3-更新实例状态为5 new Pkurg.PWorldBPM.Business.Controls.Management().StopActitvy(procInstID); //4-调用业务系统更新状态 int k2Sn = procInstID; string instanceID = ""; string formId = ""; string appId = ""; DataTable dt = new Pkurg.PWorldBPM.Business.Controls.Management().GetFlowInstance(k2Sn); if (dt != null && dt.Rows.Count > 0) { instanceID = dt.Rows[0]["InstanceID"].ToString(); formId = dt.Rows[0]["FormID"].ToString(); appId = dt.Rows[0]["AppID"].ToString(); } try { new Invoke().StopWorkFlow(k2Sn, instanceID, formId, appId); } catch (Exception ex) { Logger.Write(this.GetType(), EnumLogLevel.Info, "*****StopActitvy:" + ex.Message); } Logger.Write(this.GetType(), EnumLogLevel.Info, "*****终止流程操作结束"); BPMHelp.InsertInstanceLog("终止流程", formId.Replace("-", "").Trim(), "", Employee_Name + " - " + CurrentEmployee.EmployeeCode, HttpContext.Current.User.Identity.Name.Replace("founder\\", "")); }