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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:51:39+00:00 2026-05-14T06:51:39+00:00

I have set a property across threads before and I found this post Cross-thread

  • 0

I have set a property across threads before and I found this post Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on about getting a property.

I think my issue with the code below is setting the variable to the collection is an object therefore on the heap and therefore is just creating a pointer to the same object

So my question is besides creating a deep copy, or copying the collection into a different List object is there a better way to do the following to aviod the error during the for loop.

Cross-thread operation not valid: Control 'lstProcessFiles' accessed 
from a thread other than the thread it was created on.

Code:

    private void btnRunProcess_Click(object sender, EventArgs e)
    {
        richTextBox1.Clear();

        BackgroundWorker bg = new BackgroundWorker();
        bg.DoWork += new DoWorkEventHandler(bg_DoWork);
        bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
        bg.RunWorkerAsync();
   }


    void bg_DoWork(object sender, DoWorkEventArgs e)
    {
        WorkflowEngine engine = new WorkflowEngine();
        ListBox.SelectedObjectCollection selectedCollection=null;

        if (lstProcessFiles.InvokeRequired)
        {
            // Try #1
            selectedCollection = (ListBox.SelectedObjectCollection) 
                this.Invoke(new GetSelectedItemsDelegate(GetSelectedItems), 
                new object[] { lstProcessFiles });

            // Try #2
            //lstProcessFiles.Invoke(
            //    new MethodInvoker(delegate {
            //        selectedCollection = lstProcessFiles.SelectedItems; }));
        }
        else
        {
            selectedCollection = lstProcessFiles.SelectedItems;
        }         

        // *********Same Error on this line********************
        // Cross-thread operation not valid: Control 'lstProcessFiles' accessed 
        // from a thread other than the thread it was created on.
        foreach (string l in selectedCollection)
        {
            if (engine.LoadProcessDocument(String.Format(@"C:\TestDirectory\{0}", l)))
            {
                try
                {
                    engine.Run();                       
                    WriteStep(String.Format("Ran {0} Succussfully", l));
                }
                catch
                {
                    WriteStep(String.Format("{0} Failed", l));
                }

                engine.PrintProcess();
                WriteStep(String.Format("Rrinted {0} to debug", l));
            }
        }
    }

    private delegate void WriteDelegate(string p);
    private delegate ListBox.SelectedObjectCollection GetSelectedItemsDelegate(ListBox list);

    private ListBox.SelectedObjectCollection GetSelectedItems(ListBox list)
    {
        return list.SelectedItems;
    }
  • 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-14T06:51:40+00:00Added an answer on May 14, 2026 at 6:51 am

    Take a look at this SO question – it addresses a similar topic.

    In many UI technologies (Winforms, WPF, Silverlight) – UI elements can only be safely interacted with on the UI thread. This means that when writing multithreaded code, you need to use the mechanisms in your UI library of choice to correctly interact with UI controls. In WPF/Silverlight that would be the Dispatcher, in WinForms it requires using InvokeRequired method and BeginInvoke() to dispatch the work to the UI thread.

    In your case, it seems like you are already trying to use BeginInvoke to dispatch to the appropriate thread. I suspect the problem is that iterating over the ListBox’s collection is a cross-thread operation itself. You don’t know how the SelectedItems collection implements GetEnumerator() – most likley though it doesn’t copy the collection. So I suspect your code will have to make a copy before iterating over it – or perform the entire iteration on the UI thread.

    Making a copy of the collection isn’t to bad, but again, the copy has to be made on the UI thread – and it may need to be a deep copy since it contains other UI-owned objects (ListBoxItem). If you are on .NET 3.5, you can use LINQ to project an anonymous object in your code rather than trying to make deep copies of UI elements.

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

Sidebar

Ask A Question

Stats

  • Questions 389k
  • Answers 389k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The iPad doesn't support the translucent status bar. On iPad… May 15, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer I haven't quite figured out the answer to my original… May 15, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer Make no mistake, GWT is a Java technology. You could… May 15, 2026 at 12:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.