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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:36:02+00:00 2026-06-03T19:36:02+00:00

I want to get a control’s property from a BackgroundWorker in my form: foreach

  • 0

I want to get a control’s property from a BackgroundWorker in my form:

foreach (ListViewItem i in ListView.CheckedItems) { //error: Cross-thread operation not valid: Control 'ListView' accessed from a thread other than the thread it was created on.
    //do something with i
}

Can anyone suggest the simplest and easiest method to do this?

  • 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-03T19:36:04+00:00Added an answer on June 3, 2026 at 7:36 pm

    Let me take another stab at this…

    1.) Drag a ListView onto the Form

    2.) Drag a BackgroundWorker onto the Form

    3.) Create a method do iterate through the ListViewItem collection

    private void LoopThroughListItems()
    {
        foreach (ListViewItem i in listView1.CheckedItems)
            DoSomething(); 
    
    }
    

    4.) Add code to call LoopThroughListItems() inside the BackgroundWorker’s DoWork Event

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        LoopThroughListItems();
    }
    

    5.) In your Form Load – execute the code on the main thread (it works) then on the backgroundWorkder thread (it fails)

    private void Form1_Load(object sender, EventArgs e)
    {
        // Try it on the UI Thread - It works
        LoopThroughListItems();
    
        // Try it on a Background Thread - It fails
        backgroundWorker1.RunWorkerAsync();
    
    }
    

    6.) Modify your code to use IsInvokeRequired/Invoke

    private void LoopThroughListItems()
    {
    
        // InvokeRequired == True when executed by non-UI thread
        if (listView1.InvokeRequired)
        {
            // This will re-call LoopThroughListItems - on the UI Thread
            listView1.Invoke(new Action(LoopThroughListItems));
            return;
        }
    
        foreach (ListViewItem i in listView1.CheckedItems)
            DoSomething(); 
    }
    

    7.) Run the app again – now it works on the UI thread and the non-UI thread.

    That solve the problem. The checking IsInvokeRequired/Invoking is a common pattern you’ll get used to a lot (which is why it’s included on all Controls). If you are doing it all over the place, you can do something clever and wrap it all up – as described here: Automating the InvokeRequired code pattern

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

Sidebar

Related Questions

Basically I want to get bezier control points from a ttf font and then
I have standard .net 2.0 gridview control from which i want to get row
I'm creating a web user control and want to get some html from a
I want to get value from NSDictionary . I want to control Code key
I have a control containing some text which I want to get from a
I want to get start with DayPilot control I do not use SQLite and
I want to get the type of file uploaded using the ASP.NET FileUpload control.
Possible Duplicate: Get full path of a file with FileUpload Control I want to
Hi I didn't get an answer before so second try... I want to control
I want get as much as possible from Redis + Hiredis + libevent. I'm

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.