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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:31:53+00:00 2026-05-23T02:31:53+00:00

I need some help trying to figure what I’m doing wrong. I’m trying to

  • 0

I need some help trying to figure what I’m doing wrong. I’m trying to get a collection of items from the system log on a separate thread to keep the form from being frozen during the collection process. I can get the background worker to grab them all, but I am having some issues add them to the ListBox on the form.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

  foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
  {
     listBox1.Items.Add(
        entry.EntryType.ToString() + " - " + 
        entry.TimeWritten + "     - " + 
        entry.Source);
  }
}

Obviously this doesn’t work as expected, since there are 2 separate threads, and you can’t change objects on different threads, as I have found out. So, If someone could guide me in the right direction, I would be thankful.

  • 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-23T02:31:54+00:00Added an answer on May 23, 2026 at 2:31 am

    You should not access UI elements from non-UI thread. Run ReportProgress, which will be synced with UI thread.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
        {
            var newEntry = entry.EntryType + " - " + entry.TimeWritten + "     - " + entry.Source;
            backgroundWorker1.ReportProgress(0, newEntry);
        }
    }
    
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        var newEntry = (string)e.UserState;
        listBox1.Items.Add(newEntry);
    }
    

    Make sure you enable WorkerReportsProgress.

    backgroundWorker1.WorkerReportsProgress = true;
    

    and subscribed to ProgressChanged

    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    

    Another approach is to call Control.Invoke inside

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
        {
            var newEntry = entry.EntryType.ToString() + " - " + entry.TimeWritten + "     - " + entry.Source;
            Action action = () => listBox1.Items.Add(newEntry);
            Invoke(action);
        }
    }
    

    But with this approach you don’t need BackgroundWorker as whole point of it is to use ProgressChanged and RunWorkerCompleted event handler which are synced with the UI thread.

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

Sidebar

Related Questions

Hey all i am in need of some help trying to figure out how
I need some help trying to figure out how to format dates in perl.
Hey all, i am in need of some help trying to figure out why
I am in need of some help with trying to figure out how to
Trying to figure out datetime module and need some help. I've got a string
I need some help structuring my php. I am trying to get 3 different
I need some help. I am trying to figure out how to schedule jobs
I need some help with some CSS coding. I have been trying to get
still trying to figure out jQuery and I need some help with a sidebar
Hey all, i am in need of some help trying to figure out why

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.