Exemplo n.º 1
0
        //_________________________________________________________________________________________________
        //_________________________________________________________________________________________________
        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!
        }