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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:36:42+00:00 2026-05-18T12:36:42+00:00

I’m trying to work with Threadding and it seems to me like it’s suspiciously

  • 0

I’m trying to work with Threadding and it seems to me like it’s suspiciously difficult (I’m probably doing it wrong).

I want to load a file inside a BackgroundWorker and while that happens, “send” each new line to a separate Thread (not bgWorker). I’m using BlockingCollection and Add() each line, then I want to Take() them and process them in another thread.

Now, everything is straightforward with the BgWorker; but why is it impossible(isn’t it?) to just declare a new thread in Form1.cs and have it perform like the BgWorker? In other words, why must you create a separate WorkerClass ( http://msdn.microsoft.com/en-us/library/7a2f3ay4(VS.80).aspx )?

I’m asking this because, you can access your BlockingCollection fine from within the BackgroundWorker, but you can’t do it from a separate WorkerClass (since it’s a plain vanilla separate class). (So what’s the point of the BlockingCollection then if you can’t use it for what it’s meant?)

Also, BgWorkers have a ReportProgress(...) event/method. As far as I know, if you use that msdn example, you don’t have squat in your Thread.

What am I missing here? Please help.


PS: Before you jump and tell me that It’s not in any way more efficient to send lines to another thread, know that I’m doing this as a learning exercise. Trying to figure out how Threads work in C# and how you sync/communicate between/with them (and/or bgWorkers).

  • 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-18T12:36:43+00:00Added an answer on May 18, 2026 at 12:36 pm

    To answer your question in the title, yes “normal” threads can act like BackgroundWorker threads. You just have to create more of the wiring code yourself.

    I wrote a simple application for scanning my music collection using a manually created thread. The main body of the thread is a method that loops over all of the folders under a specified root and fires an event each time it encounters a folder that contains some mp3 files.

    I subscribe to this event in the main form of my application and update a DataGridView with the new information.

    So the thread is kicked off by the following code:

    this.libraryThread = new Thread(new ThreadStart(this.library.Build)) { IsBackground = true };
    
    // Disable all the buttons except for Stop which is enabled
    this.EnableButtons(false);
    
    // Find all the albums
    this.libraryThread.Start();
    

    The method supplied to ThreadStart does some housekeeping and then calls the method that does the work:

    private void FindAlbums(string root)
    {
        // Find all the albums
        string[] folders = Directory.GetDirectories(root);
        foreach (string folder in folders)
        {
            if (this.Stop)
            {
                break;
            }
    
            string[] files = Directory.GetFiles(folder, "*.mp3");
            if (files.Length > 0)
            {
                // Add to library - use first file as being representative of the whole album
                var info = new AlbumInfo(files[0]);
                this.musicLibrary.Add(info);
                if (this.Library_AlbumAdded != null)
                {
                    this.Library_AlbumAdded(this, new AlbumInfoEventArgs(info));
                }
            }
    
            this.FindAlbums(folder);
        }
    }
    

    When this method finishes a final LibraryFinished event is fired.

    I subscribe to these events in the main form:

    this.library.Library_AlbumAdded += this.Library_AlbumAdded;
    this.library.Library_Finished += this.Library_Finished;
    

    and in these methods add the new album to the grid:

    private void Library_AlbumAdded(object sender, AlbumInfoEventArgs e)
    {
        this.dataGridView.InvokeIfRequired(() => this.AddToGrid(e.AlbumInfo));
    }
    

    and finish off (which reenables buttons etc.):

    private void Library_Finished(object sender, EventArgs e)
    {
        this.dataGridView.InvokeIfRequired(() => this.FinalUpdate());
    }
    

    As you can see this is a lot of work which would be a whole lot simpler if I used a BackgroundWorker.

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

Sidebar

Related Questions

i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.