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 8605307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:47:51+00:00 2026-06-12T02:47:51+00:00

In my application I have a queue download list which consists of progress bars

  • 0

In my application I have a queue download list which consists of progress bars and the file names. When the user clicks a button the file name and progress bar is instantiated and added to the queue. Files download one at a time and asynchronously. What I want to do is keep all the progress bars of the files that are waiting to be downloaded yellow in color and then turn green when it is being downloaded and then turn blue when they are completed. It currently works if I have CheckForIllegalCrossThreadCalls = false; in the constructor of the custom progress bar. I want to see if there is a way to make thread safe changes to the progress bars.

I have each queue item set up as an object. The queue item objects are created from the main form code (Form1.cs) when a button is pressed and the progress bars are created in the queue item constructor, which is probably where my problem begins. The downloads are started through a function in the queue item object.

Queue Item Snippet

 public class QueueItem
 {
    public bool inProgress;
    public QueueBar bar;

    public QueueItem(args)
    {
         bar = new QueueBar();
         inProgress = false;
         // handle arguments
    }

    public void Download()
    {
        // process info
        WebClient client = new WebClient();
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadFileAsync(url, @savePath);
    }

    private long lastByte = 0;
    private long newByte = 0;
    private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        percentValue = e.ProgressPercentage;
        bar.Value = e.ProgressPercentage;
        newByte = e.BytesReceived;
    }

    private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        // change bar color
        bar.Value = 100;
    }
 }

Queue Bar Snippet

public class QueueBar : ProgressBar
{
    // variables

    public QueueBar()
    {
        this.SetStyle(ControlStyles.UserPaint, true);     
        // initialize variables
    }

    // function to change text properties
    // function to change color

    protected override void OnPaint(PaintEventArgs e)
    {
        // painting
    }
}

Main Function Snippet

public partial class Form1 : Form
{
    private List<QueueItem> qItems;
    private BackgroundWorker queue;

    private void button_Click(object sender, EventArgs e)
    {
         // basic gist of it
        qItems.Add(new QueueItem(args));
        Label tmpLabel = new Label();
        tmpLabel.Text = filename;
        tmpLabel.Dock = DockStyle.Bottm;
        splitContainerQueue.Panel2.Controls.Add(tmpLabel);
        splitContainerQueue.Panel2.Controls.Add(qItems[qItems.Count - 1].bar);

        if (!queue.IsBusy) { queue.RunWorkerAsync(); }
    }

    private void queue_DoWork(object sender, DoWorkEventArgs e)
    {
        while (qItems.Count > 0)
        {
            if (!qItems[0].inProgress && qItems[0].percentValue == 0)
            {
                qItems[0].inProgress = true;
                qItems[0].Download();
            }
            // else if statements
        }
 }

I also just tried creating a background worker to create the Queue Items and add the controls asynchronously but that doesn’t work since the split container was created on a different thread.

  • 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-12T02:47:53+00:00Added an answer on June 12, 2026 at 2:47 am

    You cannot call a UI control (created on your UI thread) from another thread safely – you need to use InvokeRequired / BeginInvoke() for such calls. When calling BeginInvoke() you’ll pass a delegate; something like this (just some sample code, yours will look slightly different):

    private void SomeEventHandler ( object oSender, EventArgs oE )
    {
        if ( InvokeRequired )
        {
            MethodInvoker oDelegate = (MethodInvoker) delegate
            {
                SomeEventHandler ( oSender, oE );
            };
    
            BeginInvoke ( oDelegate );
            return;
        }
        else
        {
            // already on the correct thread; access UI controls here
        }
    }
    

    You also cannot create your progress bars away from the UI thread – you need to create all your controls as part of your UI and then if you need to access these progress bars from your queue items, you’ll have to pass in a reference to the progress bar. When you try to access the progress bar, you’ll do

    if ( bar.InvokeRequired ) { ... }
    

    to determine if you’re trying to call it from the right thread.

    The reason for this mess is because controls handle many of their property updates through messages and those messages must be delivered synchronously, in the correct order. The only way to ensure this (without some very complex coding) is to create all controls on the same thread where the thread runs a message pump.

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

Sidebar

Related Questions

In our application we have a Queue which is defined as the following: private
I have an application where user can download documents. User has option to download
We have an application that is essentially implementing its own messaging queue. When a
I have a server application receiving messages from a JMS queue. And client applications
In my console application have an abstract Factory class Listener which contains code for
I have application which uses Sherlock ActionBar package. The application uses platform-specific behavior for
I have an application, that uses scriptaculous' effects queue to render the view of
I have an application that creates a job queue, and then multiple threads execute
Suppose I have an application fed by a MQ queue. When the application receives
I have a Queue configured in the Rational Application Developer for WebSphere, using a

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.