//_________________________________________________________________________________________________ //_________________________________________________________________________________________________ public static void LoadServers(Dictionary <string, cWorker> dWorkers) { int i = 1; dFIMServers = new Dictionary <string, FIMServer>(); foreach (string s in Config.dProfiles.Keys) { if (!dFIMServers.ContainsKey(Config.dProfiles[s]["AliasSrv"].ToString())) { FIMServer oFS = new FIMServer(); oFS.label = Config.dProfiles[s]["AliasSrv"].ToString(); oFS.srv = Config.dProfiles[s]["Server"].ToString(); oFS.usr = Config.dProfiles[s]["Usr"].ToString(); oFS.pwd = Config.dProfiles[s]["Pwd"].ToString(); oFS.MAs = new List <FIMMA>(); dFIMServers.Add(Config.dProfiles[s]["AliasSrv"].ToString(), oFS); dWorkers["" + i++].oFIMServer = oFS; } FIMMA oMA = new FIMMA(); oMA.profileKey = s; oMA.label = Config.dProfiles[s]["AliasMA"].ToString(); oMA.name = Config.dProfiles[s]["MA"].ToString(); oMA.pflE = Config.dProfiles[s]["E"].ToString(); oMA.pflDI = Config.dProfiles[s]["DI"].ToString(); oMA.pflDS = Config.dProfiles[s]["DS"].ToString(); oMA.pflFI = Config.dProfiles[s]["FI"].ToString(); oMA.pflFS = Config.dProfiles[s]["FS"].ToString(); oMA.pflEDIS = Config.dProfiles[s]["EDIS"].ToString(); oMA.pflEFIS = Config.dProfiles[s]["EFIS"].ToString(); //dFIMServers[s].dMAs.Add(oMA.label, oMA); dFIMServers[Config.dProfiles[s]["AliasSrv"].ToString()].MAs.Add(oMA); } }
//_________________________________________________________________________________________________ //_________________________________________________________________________________________________ protected void myWorker1_DoWork(object sender, DoWorkEventArgs e) { changes = false; BackgroundWorker sendingWorker = (BackgroundWorker)sender; //Capture the BackgroundWorker that fired the event FIMServer FIMSrv = (FIMServer)e.Argument; //Collect the array of objects the we recived from the main thread int maxValue = (int)FIMSrv.num; //Get the numeric value from inside the objects array, don't forget to cast StringBuilder sb = new StringBuilder(); //Declare a new string builder to store the result. // Conect to servers string sMsg = "Connecting " + FIMSrv.label + ": " + WmiHelper.ConnectServer(FIMSrv); sb.Append(string.Format("{0}{1}", sMsg, Environment.NewLine)); Log(sb.ToString()); ResultProfile result = new ResultProfile(); result.r = "success"; // Clear logs WmiHelper.ClearRuns(FIMSrv); // Execute profiles for (int i = 1; i <= maxValue && result.r == "success"; i++) //Start a for loop { if (!sendingWorker.CancellationPending) //At each iteration of the loop, check if there is a cancellation request pending { result = PerformHeavyOperation(FIMSrv); sMsg = Environment.NewLine + "Result: " + FIMSrv.label + " [" + FIMSrv.lastRun + "]: " + result.r + result.desc + Environment.NewLine; //if (sMsg.Contains("error")) e.Result = "Error"; sb.Append(sMsg); //Append the result to the string builder sendingWorker.ReportProgress(i); //Report our progress to the main thread Log(sb.ToString(), result.changes); System.Threading.Tasks.Task.Delay(5000); } else { e.Cancel = true; //If a cancellation request is pending,assgine this flag a value of true break; // If a cancellation request is pending, break to exit the loop } changes |= (result.changes > 0); } e.Result = sb.ToString();// Send our result to the main thread! }
//_________________________________________________________________________________________________ //_________________________________________________________________________________________________ public void Run(FIMServer FIMSrv) { if (!myWorker1.IsBusy) //Check if the worker is already in progress { if (lblStatus != null) { lblStatus.Tag = "START"; lblStatus.Text = "Waiting..."; } if (btnStart != null) { btnStart.Enabled = false;//Disable the Start button btnStart.Text = "Cancel"; btnStart.Enabled = true; } if (textBox1 != null) { this.textBox1.ForeColor = System.Drawing.Color.Black; } myWorker1.RunWorkerAsync(FIMSrv);//Call the background worker } }
//_________________________________________________________________________________________________ //_________________________________________________________________________________________________ private ResultProfile PerformHeavyOperation(FIMServer FIMSrv) { ResultProfile r = new ResultProfile(); r.r = "success"; if (FIMSrv.runPf == "DJ") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflEDIS); } } if (FIMSrv.runPf == "FJ") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflEFIS); } } if (FIMSrv.runPf == "DS") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflE); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflDI); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflDS); } } } if (FIMSrv.runPf == "FS") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflE); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflFI); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflFS); } } } if (FIMSrv.runPf == "DSNOE") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflDI); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflDS); } } } if (FIMSrv.runPf == "FSNOE") { for (int i = 0; i < FIMSrv.MAs.Count && r.r == "success"; i++) { if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflFI); } if (r.r == "success") { r = WmiHelper.RunProfile(FIMSrv, i, FIMSrv.MAs[i].pflFS); } } } return(r); }