c# – Call backgroundworker in static method


I creating simple permutation code using c# app. Now I want to display a simple progressbar. Now if the program execute I want to check if the background is busy else I call the backgroundworker and show how many percent remaining.

The code below I call when permutation code is execute.

static IEnumerable<IEnumerable<int>> GetPermutations(IEnumerable<int> indices, int limit)
        {
            if (limit == 1)
            {
                foreach (var index in indices)
                    yield return new[] { index };
                yield break;
            }

            var indicesList = indices.ToList();
            var subPermutations = GetPermutations(indices, limit - 1);

            foreach (var index in indicesList)
            {
                foreach (var subPerm in subPermutations)
                {
                    if (!subPerm.Contains(index))
                    {
                        yield return subPerm.Append(index);

                        //Cursor.Current = Cursors.WaitCursor;//for waiting..

                        //try
                        if (backgroundWorker1.IsBusy != true)
                        {
                            // Start the asynchronous operation.
                            backgroundWorker1.RunWorkerAsync();
                        }

                    }
                }
            }
        }

Error image: image

and this is the backgroundworker do work code.

private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;

          for (int i = 1; i <= 10; i++)
          {
              if (worker.CancellationPending == true)
              {
                  e.Cancel = true;
                  break;
              }
              else
              {
                  // Perform a time consuming operation and report progress.
                  System.Threading.Thread.Sleep(500);
                  worker.ReportProgress(i * 10);
              }
          }
      }

My problem is, I dont know how to call backgroundWorker1 inside in static method. Please click the image above to see my error.

Leave a Reply

Your email address will not be published. Required fields are marked *