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

  • Home
  • SEARCH
  • 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 6826071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:06:16+00:00 2026-05-26T22:06:16+00:00

I just got into WPF and am currently trying my luck with the Background

  • 0

I just got into WPF and am currently trying my luck with the Background worker, so I figured I’d just open any file using the FileOpenDialog, loop through all the bytes inside the file and report the total progress via worker.ReportProgress in percentage … alas, this only works for like ~20 times and then it gets really stuck and suddenly stops at 100%.

Here’s my code:

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 Microsoft.Win32;
using System.IO;
using System.Threading;
using System.ComponentModel;

namespace BitStream
{
public partial class MainWindow : Window
{
    private int bytes = 0;
    private long length = 0;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void selectFile_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        OpenFileDialog ofd = new OpenFileDialog();
        if ((bool)ofd.ShowDialog())
        {
            FileInfo fi = new FileInfo(ofd.FileName);
            this.length = fi.Length;
            bw.DoWork += bw_DoWork;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.ProgressChanged += bw_ProgressChanged;
            bw.WorkerReportsProgress = true;
            Stream str = ofd.OpenFile();

            bw.RunWorkerAsync(str);
        }
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Stream str = (Stream)e.Argument;
        int singleByte = 0;
        this.Dispatcher.Invoke(
                new Action(() =>
                {
                    int currentProgress = 0;
                    while ((singleByte = str.ReadByte()) != -1)
                    {

                        label1.Content = singleByte;
                        bytes++;

                        currentProgress = Convert.ToInt32(((double)bytes) / length * 100);
                        if (currentProgress > progress)
                        {
                            progress = currentProgress;
                            ((BackgroundWorker)sender).ReportProgress(progress);
                            Thread.Sleep(100);
                        }
                    }
                }

            ), System.Windows.Threading.DispatcherPriority.Render);
    }

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label2.Content = e.ProgressPercentage + "% completed";
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    }
}

}

Labels 1 and 2 are there for showing the current byte and the current progress in %.

Feel free to also criticize every other aspect of my code, I just got started with WPF today.

Edited DoWork-Method:

 private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Stream str = (Stream)e.Argument;
        int singleByte = 0;

        int currentProgress = 0;
        while ((singleByte = str.ReadByte()) != -1)
        {
            this.Dispatcher.Invoke(
                new Action(() =>
                {
                    label1.Content = singleByte;
                }), System.Windows.Threading.DispatcherPriority.Render);
            bytes++;

            currentProgress = Convert.ToInt32(((double)bytes) / length * 100);
            if (currentProgress > progress)
            {
                progress = currentProgress;
                this.Dispatcher.Invoke(
                new Action(() =>
                {
                    ((BackgroundWorker)sender).ReportProgress(progress);


                }), System.Windows.Threading.DispatcherPriority.Render);
                Thread.Sleep(500);        
            }
        }
    }

Thanks,

Dennis

  • 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-05-26T22:06:17+00:00Added an answer on May 26, 2026 at 10:06 pm

    So assuming you really want to make a cross thread call for each byte (which I wouldn’t recommend), the code would look something like:

    private void bw_DoWork(object sender, DoWorkEventArgs e) 
    { 
        Stream str = (Stream)e.Argument; 
        int singleByte = 0; 
        int currentProgress = 0; 
        while ((singleByte = str.ReadByte()) != -1) 
        { 
    
           bytes++; 
           this.Dispatcher.Invoke( 
                new Action(() => 
                { 
                        label1.Content = singleByte; 
                } 
    
            ), System.Windows.Threading.DispatcherPriority.Render); 
    
            currentProgress = Convert.ToInt32(((double)bytes) / length * 100); 
            if (currentProgress > progress) 
            { 
                progress = currentProgress; 
                ((BackgroundWorker)sender).ReportProgress(progress); 
                Thread.Sleep(100); 
            } 
        } 
    } 
    

    The idea being that you can only manipulate a DispatcherObject on the thread on which it was created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just got into using Omniauth for logging in with Twitter and Facebook and
I just recently got into using vim, and am having great fun adding load
Just got into using Screen for remote pair-programming purposes with Vim. Was wondering if
I am a recreational pythonista who just got into pyCUDA. I am trying to
I've recently been doing a lot of Objective-C programming, and just got back into
I am running into a problem that just doesn't seem right. I've got a
Just got a question about generics, why doesn't this compile when using a generic
I just got my azure invitation code...yay! Are there any official samples for windows
I just got into a debate with one of my coworkers about checking for
I got into the habit of removing trailing white spaces from my source file.

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.