public void UpdateStatus(string statusStripMessage, bool incrementProgress = true) { //SyncLock ThreadLockObj bool stillAddingFunctions = MatManFunctionCollection.GetObject().StillAddingFunctions; //int functionsRemainingInQueue = PWFunctionCollection.GetObject().Count; int totalFunctionsAddedToQueue = MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue + 1; int totalProcessedBySAP = SAPRequest.GetObject().TotalProcessedBySAP; this.ElapsedTime = DateTime.Now - this.ProcessStartTime; if (totalProcessedBySAP < 1) { // report progress of RTD reading functions into batch if (ElapsedTime.TotalSeconds > 0) { // Update the CalculationCount text box this.uxCalculationCountTextBox.Text = totalFunctionsAddedToQueue.ToString() + " functions in processing queue..."; // Update the Balance Records Text Box this.uxBalanceRecordsSummedTextBox.Text = totalFunctionsAddedToQueue.ToString() + "...pending"; // Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalFunctionsAddedToQueue / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + " at " + processRate.ToString("0.00") + " cells/sec"; StatusStrip.Text = statusStripMessage; } } else if (totalProcessedBySAP >= 1) { if (ElapsedTime.TotalSeconds > 0) { //Update the CalculationCount text box this.uxCalculationCountTextBox.Text = totalFunctionsAddedToQueue.ToString() + " functions being processed by SAP..."; // Update the Balance Records Text Box this.uxBalanceRecordsSummedTextBox.Text = totalProcessedBySAP.ToString(); // Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalProcessedBySAP / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + " at " + processRate.ToString("0.00") + " cells/sec"; StatusStrip.Text = statusStripMessage; } } this.Refresh(); }
public static void ClearPreviousRun() { MatManFunctionCollection.GetObject().Clear(); MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue = 0; SAPRequest.GetObject().TotalProcessedBySAP = 0; SAPRequest.ReturnValuesList.Clear(); //MatManCalcEngine.GetObject().CurrentFunctionsByCellAddress.Clear(); //MatManCalcEngine.GetObject().ConnectDataCount = 0; //MatManCalcEngine.GetObject().TopicCount = 0; //MatManCalcEngine.GetObject().UserInitiatedCalc = true; }
/// <summary> /// Make accessor Thread-safe /// </summary> /// <returns></returns> public static MatManFunctionCollection GetObject() { if (MatManFunctionCollection.m_pwFunctionCollection == null) { lock (syncRoot) { if (MatManFunctionCollection.m_pwFunctionCollection == null) { MatManFunctionCollection.m_pwFunctionCollection = new MatManFunctionCollection(); } } } return(MatManFunctionCollection.m_pwFunctionCollection); }
/// <summary> /// Sends the Batch to SAP - This would be part of processing functions phase /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void myBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { this.ProcessStartTime = DateTime.Now; MatManFunctionCollection.GetObject().Clear(); MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue = 0; //SAPRequest.GetObject().FunctionBatch.Clear() SAPRequest.GetObject().TotalProcessedBySAP = 0; m_cancelOperation = false; Thread.Sleep(2000); while (true) { if ((MatManFunctionCollection.GetObject().Count > 0 && m_executeFunctions && !m_isRunning)) { m_isRunning = true; FunctionExecutionType functionType = (FunctionExecutionType)Properties.Settings.Default.FunctionExecutionType; SAPRequest.GetObject().ProcessSAPRequests(functionType, Settings.Default.MaximumBatchSize); } if (m_cancelOperation) { MatManFunctionCollection.GetObject().StillAddingFunctions = true; m_executeFunctions = false; m_isRunning = true; break; // TODO: might not be correct. Was : Exit While } System.Threading.Thread.Sleep(250); //' Breaks us out of while loop if ((SAPRequest.GetObject().TotalProcessedBySAP == MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue) && m_Progress == 100) { MatManFunctionCollection.GetObject().StillAddingFunctions = true; m_executeFunctions = false; break; } if ((MatManFunctionCollection.GetObject().Count == 0)) { break; } } }
/// <summary> /// OnLoad is overidden to provide localization strings for the form /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { // FunctionAdded & Function Removed EventHandlers MatManFunctionCollection.GetObject().OnFunctionAdded += functionCollection_OnFunctionAdded; MatManFunctionCollection.GetObject().OnFunctionRemoved += functionCollection_OnFunctionRemoved; //// FunctionProcessed and BatchCompleted EventHandlers SAPRequest.GetObject().FunctionProcessedBySAP += sapRequest_FunctionProcessedBySAP; m_connectDataCount = 0; this.uxCalculationTimeLabel.Text = iiiwave.MatManLib.Localization.Localize.ReturnProgressDataForm_uxCalculationTimeLabel_Text; this.uxBalanceRecordsSummedLabel.Text = iiiwave.MatManLib.Localization.Localize.ReturnProgressDataForm_uxBalanceRecordsSummedLabel_Text; this.uxCalculationCountLabel.Text = iiiwave.MatManLib.Localization.Localize.ReturnProgressDataForm_uxCalculationCountLabel_Text; this.uxCancelButton.Text = iiiwave.MatManLib.Localization.Localize.ReturnProgressDataForm_uxCancelButton_Text; if (Settings.Default.FunctionExecutionType == (int)FunctionExecutionType.RetrievingData) { this.Text = "Retrieving Data"; } else if (Settings.Default.FunctionExecutionType == (int)FunctionExecutionType.ValidateData) { this.Text = "Validating Data"; } else { this.Text = "Posting Data"; } this.uxCancelButton.Enabled = false; m_cancelOperation = false; // Initialize Form this.uxProgressBar.Value = 0; this.m_Progress = 0; // Initialize counters and booleans MatManFunctionCollection.GetObject().Clear(); // Start the background worker myBackgroundWorker.WorkerReportsProgress = true; myBackgroundWorker.WorkerSupportsCancellation = true; myBackgroundWorker.RunWorkerAsync(); }
protected override void OnFormClosing(FormClosingEventArgs e) { if (m_cancelOperation) { myBackgroundWorker.CancelAsync(); while ((myBackgroundWorker.IsBusy)) { Thread.Sleep(500); continue; } Thread.Sleep(500); OnDataFormClosing?.Invoke(); // Remove event listeners MatManFunctionCollection.GetObject().OnFunctionAdded -= functionCollection_OnFunctionAdded; MatManFunctionCollection.GetObject().OnFunctionRemoved -= functionCollection_OnFunctionRemoved; SAPRequest.GetObject().FunctionProcessedBySAP -= sapRequest_FunctionProcessedBySAP; Thread.Sleep(500); MessageBox.Show(this, "Process has been cancelled"); m_cancelOperation = false; // Clear out PWFunctionCollection completely MatManFunctionCollection.GetObject().Dispose(); // Clear out SAPRequest completely SAPRequest.GetObject().Dispose(); } else { this.uxProgressBar.Value = 100; this.Refresh(); Application.DoEvents(); Thread.Sleep(1000); this.uxProgressBar.Value = 100; this.uxToolStripStatusLabel.Text = "Done"; this.StatusStrip.BackColor = Color.FromArgb(80, 161, 216); this.Refresh(); Application.DoEvents(); OnDataFormClosing?.Invoke(); // Remove event listeners MatManFunctionCollection.GetObject().OnFunctionAdded -= functionCollection_OnFunctionAdded; MatManFunctionCollection.GetObject().OnFunctionRemoved -= functionCollection_OnFunctionRemoved; SAPRequest.GetObject().FunctionProcessedBySAP -= sapRequest_FunctionProcessedBySAP; // Clear out PWFunctionCollection completely MatManFunctionCollection.GetObject().Dispose(); // Clear out SAPRequest completely SAPRequest.GetObject().Dispose(); Thread.Sleep(2000); OnUpdateValues?.Invoke(); } base.OnFormClosing(e); //FileLogger.WriteEntry(System.Reflection.MethodBase.GetCurrentMethod().Name + " " + DateTime.Now.ToString("HH:mm:ss dd-MMM-yyyy") + " -- Trace Type: Stop ") }
private void myBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { lock (ThreadLockObj) { bool stillAddingFunctions = MatManFunctionCollection.GetObject().StillAddingFunctions; int remainingInQueue = MatManFunctionCollection.GetObject().Count; int totalAddedToQueue = MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue; int totalProcessedBySAP = SAPRequest.GetObject().TotalProcessedBySAP; decimal progressDec = ((decimal)totalProcessedBySAP / (decimal)totalAddedToQueue) * 100; m_Progress = (int)progressDec; try { this.ElapsedTime = DateTime.Now - this.ProcessStartTime; if (m_cancelOperation) { this.uxProgressBar.Value = 100; this.Refresh(); Application.DoEvents(); } else if (stillAddingFunctions) { if (totalProcessedBySAP < totalAddedToQueue) { //this.uxToolStripStatusLabel.Text = Convert.ToString(e.UserState); this.uxCalculationCountTextBox.Text = "SAP is processing functions..."; this.uxBalanceRecordsSummedTextBox.Text = "Total Functions Queued: " + totalAddedToQueue.ToString() + " -- Processing: " + totalProcessedBySAP.ToString(); // Items being processed by SAP - Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalProcessedBySAP / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = totalProcessedBySAP.ToString() + " Functions processed by SAP at " + ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + "- Process Rate: " + processRate.ToString("0.00") + " cells/sec"; this.uxProgressBar.Value = m_Progress; this.Refresh(); // (2) Done } else if (totalProcessedBySAP == totalAddedToQueue) { //this.uxToolStripStatusLabel.Text = Convert.ToString(e.UserState); this.uxCalculationCountTextBox.Text = "SAP is processing functions..."; this.uxBalanceRecordsSummedTextBox.Text = "Total Functions Queued: " + totalAddedToQueue.ToString() + " -- Complete: " + totalProcessedBySAP.ToString(); // Items being processed by SAP - Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalProcessedBySAP / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = totalProcessedBySAP.ToString() + " processed at " + ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + "- Process Rate: " + processRate.ToString("0.00") + " cells/sec"; this.uxProgressBar.Value = m_Progress; this.Refresh(); Application.DoEvents(); } // RTD has COMPLETED adding functions to the queue. The background worker sends remaining functions to SAP to be processed // Excel is done reading functions - process the remaining batch } else if (!stillAddingFunctions) { this.uxCancelButton.Enabled = true; if (totalProcessedBySAP < totalAddedToQueue) { //this.uxToolStripStatusLabel.Text = Convert.ToString(e.UserState); this.uxCalculationCountTextBox.Text = "SAP is processing functions..."; this.uxBalanceRecordsSummedTextBox.Text = "Total Functions Queued: " + totalAddedToQueue.ToString() + " -- Complete: " + totalProcessedBySAP.ToString(); // Items being processed by SAP - Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalProcessedBySAP / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = totalProcessedBySAP.ToString() + " processed at " + ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + "- Process Rate: " + processRate.ToString("0.00") + " cells/sec"; if ((m_Progress < 90)) { this.uxProgressBar.Value = m_Progress; } else { this.uxProgressBar.Value = 100; } this.Refresh(); //Application.DoEvents(); } else if (totalProcessedBySAP == totalAddedToQueue) { //this.uxToolStripStatusLabel.Text = Convert.ToString(e.UserState); this.uxCalculationCountTextBox.Text = "SAP is processing functions..."; this.uxBalanceRecordsSummedTextBox.Text = "Total Functions Queued: " + totalAddedToQueue.ToString() + " -- Complete: " + totalProcessedBySAP.ToString(); // Items being processed by SAP - Update the CalculationTimeTextBox decimal processRate = Convert.ToDecimal(totalProcessedBySAP / ElapsedTime.TotalSeconds); uxCalculationTimeTextBox.Text = totalProcessedBySAP.ToString() + " processed at " + ElapsedTime.Minutes.ToString() + " : " + ElapsedTime.Seconds.ToString() + "- Process Rate: " + processRate.ToString("0.00") + " cells/sec"; this.uxProgressBar.Value = 100; this.Refresh(); Application.DoEvents(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message) } } }
/// <summary> /// Extension member for PWFunctionCollection /// This member adds a series of grouped functions, based on sorting criteria, to the List /// A group represents a grid of tabular values to be passed to SAP in a single network function call /// </summary> /// <param name="functionCollection"></param> /// <returns></returns> public static List <IFunctionGroup> GetFunctionGroups(this MatManFunctionCollection functionCollection) { List <IFunctionGroup> groupList = new List <IFunctionGroup>(); MatManCompareFunction myCompare = new MatManCompareFunction(); SortedList <string, CostPlan> loadCostPlanList = new SortedList <string, CostPlan>(myCompare); SortedList <string, ActivityPlan> loadActivityPlanList = new SortedList <string, ActivityPlan>(myCompare); // Future function Lists //#Region Build function lists from the function collection (PWFunctionCollection) using dequeue while (functionCollection.Count > 0) { IMatManFunction myFunction = functionCollection.Dequeue(); Type myType = myFunction.GetType().BaseType; if (myFunction.GetType().BaseType == typeof(IPlanningFunction)) { switch (((IPlanningFunction)myFunction).FunctionType) { case PlanningFunctionType.CostPlan: { loadCostPlanList.Add(myFunction.Signature, (CostPlan)myFunction); break; } case PlanningFunctionType.ActivityPlan: { loadActivityPlanList.Add(myFunction.Signature, (ActivityPlan)myFunction); break; } default: break; } } } //#End Region // index for all elements int functionIndex = 0; #region CostPlan grouping { var costCenterElements = from function in loadCostPlanList.Values group function by new { ControllingArea = function.ControllingArea, FiscalYear = function.FiscalYear, PeriodFrom = function.PeriodFrom, PeriodTo = function.PeriodTo, Version = function.Version, DocumentHeaderText = function.DocumentHeaderText, PlanningCurrency = function.PlanningCurrency, Delta = function.Delta } into functionGroup select functionGroup; int costGroupSize = 0; int costObjectIndex = 0; int costValueIndex = 0; int count = costCenterElements.AsEnumerable().Count(); //int i = 0; for (int i = 0; i < count; i++) { var g = costCenterElements.ElementAt(i); int groupCount = g.Count(); //int j = 0; for (int j = 0; j < groupCount;) { costGroupSize = Math.Min((Settings.Default.MaximumBatchSize - 1), (groupCount - 1)); // Build a FunctionGroup to add to the list, if greater than max group size - begin new group PlanningFunctionGroup myGroup = new PlanningFunctionGroup(PlanningFunctionType.CostPlan); // increment Object Index (Index Structure) for (int k = 0; k <= costGroupSize; k++) { if (j < groupCount) { var item = costCenterElements.ElementAt(i).ElementAt(j); costObjectIndex++; costValueIndex++; functionIndex++; // increment Value Index (Index Structure) item.ObjectIndex = costObjectIndex; item.ValueIndex = costValueIndex; item.Index = functionIndex; myGroup.FunctionList.Add(item); j++; if (j >= groupCount) { break; } } else { break; } } groupList.Add(myGroup); } } } #endregion #region ActivityPlan grouping { var activityCenterElements = from function in loadActivityPlanList.Values group function by new { CompanyCode = function.ControllingArea, FiscalYear = function.FiscalYear, PeriodFrom = function.PeriodFrom, PeriodTo = function.PeriodTo, Version = function.Version, DocumentHeaderText = function.DocumentHeaderText, PlanningCurrency = function.PlanningCurrency, Delta = function.Delta } into functionGroup select functionGroup; int activityGroupSize = 0; int activityObjectIndex = 0; int activityValueIndex = 0; int activityAttributeIndex = 0; int count = activityCenterElements.AsEnumerable().Count(); //int i = 0; for (int i = 0; i < count; i++) { var g = activityCenterElements.ElementAt(i); int groupCount = g.Count(); //int j = 0; for (int j = 0; j < groupCount;) { activityGroupSize = Math.Min((Settings.Default.MaximumBatchSize - 1), (groupCount - 1)); // Build a FunctionGroup to add to the list, if greater than max group size - begin new group PlanningFunctionGroup myGroup = new PlanningFunctionGroup(PlanningFunctionType.ActivityPlan); // increment Object Index (Index Structure) for (int k = 0; k <= activityGroupSize; k++) { if (j < groupCount) { var item = activityCenterElements.ElementAt(i).ElementAt(j); activityObjectIndex++; activityValueIndex++; activityAttributeIndex++; functionIndex++; item.ObjectIndex = activityObjectIndex; item.ValueIndex = activityValueIndex; item.AttributeIndex = activityAttributeIndex; item.Index = functionIndex; myGroup.FunctionList.Add(item); j++; if (j >= groupCount) { break; } } else { break; } } groupList.Add(myGroup); } } } #endregion return(groupList); }
public void ProcessSAPRequests(FunctionExecutionType _executionType, int _maxBatchSize) { lock (syncRoot) { //m_returnValuesDictionary.Clear(); m_executionType = _executionType; m_maximumBatchSize = _maxBatchSize; m_batchProcessTimeStart = DateTime.Now; m_functionCount = 0; //' reset to 0 for each batch //m_currentBatchNumberProcessed = 0 // reset to 0 for each batch m_updateBatchComplete = false; // reset for each batch m_batchNumber = 1; if (MatManFunctionCollection.GetObject().Count > 0) { // ToDo: Log entry here // Get all of the batched functions from the PWFunctionCollection lock (syncRoot) { m_functionBatchGroups = MatManFunctionCollection.GetObject().GetFunctionGroups(); } //For Each _functionGroup As FunctionGroup In m_planningFunctionBatchGroups for (int index = 0; index <= (m_functionBatchGroups.Count - 1); index++) { IFunctionGroup _functionGroup = m_functionBatchGroups[index]; // Start of Batch Process if (!m_operationCancelled) { if (_functionGroup.GetType() == typeof(PlanningFunctionGroup)) { switch (((PlanningFunctionGroup)_functionGroup).FunctionType) { case PlanningFunctionType.CostPlan: if (m_executionType == FunctionExecutionType.ValidateData) { CostPlan.ValidateSAPData(((PlanningFunctionGroup)_functionGroup), m_functionCount); } else { CostPlan.PostSAPData(((PlanningFunctionGroup)_functionGroup), m_functionCount); } break; case PlanningFunctionType.ActivityPlan: if (m_executionType == FunctionExecutionType.ValidateData) { ActivityPlan.ValidateSAPData(((PlanningFunctionGroup)_functionGroup), m_functionCount); } else { ActivityPlan.PostSAPData(((PlanningFunctionGroup)_functionGroup), m_functionCount); } break; default: break; } } else if (_functionGroup.GetType() == typeof(QueryFunctionGroup)) { } } else { //m_currentBatchNumberProcessed = 0; m_totalProcessedBySAP = 0; m_batchNumber = 1; MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue = 0; MatManFunctionCollection.GetObject().Clear(); FunctionProcessedBySAP?.Invoke(this, new FunctionProcessedBySAPEventArgs(1, 1)); break; } m_functionCount = m_functionCount + _functionGroup.FunctionList.Count - 1; //m_currentBatchNumberProcessed = m_currentBatchNumberProcessed + _functionGroup.FunctionList.Count; m_totalProcessedBySAP = m_totalProcessedBySAP + _functionGroup.FunctionList.Count; m_totalAddedToQueue = MatManFunctionCollection.GetObject().TotalFunctionsAddedToQueue; // Increment Batch Number m_batchNumber++; try { //Invoke the FunctionProcessed event FunctionProcessedBySAP?.Invoke(this, new FunctionProcessedBySAPEventArgs(m_totalAddedToQueue, m_totalProcessedBySAP)); } catch (Exception e1) { } } // end foreach // Re-assign the LAST BATCH UPDATE time to now. m_batchProcessTimeComplete = DateTime.Now; // Calculate the time to complete the batch process m_currentBatchProcessTime = (DateTime)m_batchProcessTimeComplete - (DateTime)m_batchProcessTimeStart; //Update the Batch to COMPLETE processing m_updateBatchComplete = true; } else { //PWCalculationEngine.AcceptNewCalcs = True } } }