Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:51:20+00:00 2026-06-01T15:51:20+00:00

I am learning backgroundworker class in WPF. The code below is in file MainWindow.xaml.cs

  • 0

I am learning backgroundworker class in WPF. The code below is in file MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;

namespace FrontEnd
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BackgroundWorker backGroundWorker;

        public MainWindow()
        {
            InitializeComponent();
            backGroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker"));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            button1.IsEnabled = false;
            Flow pro = new Flow(20,10);
            backGroundWorker.RunWorkerAsync(pro);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Flow pro = (Flow)e.Argument;
           e.Result = pro.NaturalNumbers();
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value= e.ProgressPercentage;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((int)e.Result == 1) MessageBox.Show("DONE");
            progressBar1.Value = 0;
        }
    }
}

The code below is in file Flow.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace FrontEnd
{
    class Flow
    {
        long i;
        //private int x,y;
        public int X
        {
            get; set;
        }
        public int Y
        {
            get; set;
        }

        public Flow(int x, int y)
        {
            X = x;
            Y = y;
        }
        public int NaturalNumbers()
        {
            for (i = 0; i < 9999; i++)
            {
                Console.WriteLine(i);
                long iteration = i * 100 / 9999;
                if ((i % iteration == 0) &&
                (backgroundWorker != null) && backgroundWorker.WorkerReportsProgress)
                {
                    backgroundWorker.ReportProgress(iteration);
                }
            }
            return 1;
        }
    }
}

Error : The name ‘backgroundWorker’ does not exist in the current
context

How can I make progress bar working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:51:21+00:00Added an answer on June 1, 2026 at 3:51 pm

    Here’s a simple example that works:

    public partial class BackgroundWorkerPage : Page
    {
        private readonly BackgroundWorker _worker = new BackgroundWorker();
    
        public BackgroundWorkerPage()
        {
            InitializeComponent();
    
            _worker.DoWork += WorkerOnDoWork;
            _worker.WorkerReportsProgress = true;
            _worker.ProgressChanged += WorkerOnProgressChanged;
        }
    
        private void WorkerOnProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs)
        {
            progressBar.Value = progressChangedEventArgs.ProgressPercentage;
        }
    
        private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(50);
                _worker.ReportProgress(i);
            }
        }
    
        private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            _worker.RunWorkerAsync();
        }
    }
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ProgressBar x:Name="progressBar" Height="23" Minimum="0" Maximum="100"/>
        <Button Grid.Row="1" Height="23" Content="Start" Click="Button_Click_1"/>
    </Grid>
    

    And you need to change your code a bit

    private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
    {
        var flow = new Flow(_worker);
        flow.NaturalNumbers();
    }
    
    internal class Flow
    {
        private readonly BackgroundWorker _worker;
    
        public Flow(int x, int y)
        {
            X = x;
            Y = y;
        }
    
        public Flow(BackgroundWorker worker)
        {
            _worker = worker;
        }
    
        public int X { get; set; }
        public int Y { get; set; }
    
        public int NaturalNumbers()
        {
            for (int i = 0; i <= 9999; i++)
            {
                int iteration = i*100/9999;
    
                // your if(...) fails with divide by zero exception
    
                _worker.ReportProgress(iteration);
            }
    
            return 1;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning to build multithreading WPF applications I read about some restrictions in using BackgroundWorker
learning about loops (still a beginner) in VB.net. I have got the below code
Learning Objective-C and reading sample code, I notice that objects are usually created using
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
Learning jquery, so please be kind :) Using PHP, I have a table with
Learning C++ and see the class laid out like this: class CRectangle { int
(learning c#) I have a C# WPF application which when a certain form is
Learning to use Ruby Threads for transportability of code between different OS platforms. The
For learning purposes, I'm trying to make a function using Python that takes in
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.