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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:46:42+00:00 2026-05-26T17:46:42+00:00

I have a List<Polyline> that I need to generate in a second thread so

  • 0

I have a List<Polyline> that I need to generate in a second thread so as not to lose GUI responsiveness. After The thread is complete I need to access the List<Polyline> in the GUI thread to display. This is where I am running into a problem. I am invoking the copy logic on the UI thread with an anonymous method, copying the polylines into a List<Polyline> already initialized on the UI thread. I am getting “The calling thread cannot access this object because a different thread owns it.”

I have tried Clone and DeepCopy extension methods to copy from the thread List to the UI list but Polyline is not serializable.

    Class ThreadedExecuter<T> where T : class
    {
        public delegate void CallBackDelegate(T returnValue);
        public delegate T MethodDelegate();
        private CallBackDelegate callback;
        private MethodDelegate method;

        private Thread t;

        public ThreadedExecuter(MethodDelegate method, CallBackDelegate callback)
        {
            this.method = method;
            this.callback = callback;
            t = new Thread(this.Process);
            //XPSDocument requires STA
            t.SetApartmentState(ApartmentState.STA);
        }
        public void Start()
        {
            t.Start();
        }
        private void Process()
        {
            T stuffReturned = method();
            callback(stuffReturned);
        }
    }



    void StartXPStoPolyThread()
    {
        ThreadedExecuter<List<Polyline>> executer = new ThreadedExecuter<List<Polyline>>(GeneratePolyFromXPS, PolyFromXPSComplete);
        executer.Start();
    }

    List<Polyline> GeneratePolyFromXPS()
    {

        string file = @"C:\Users\Company\Desktop\shapes6.xps";
        Company.XPStoPolyline.XPStoPolylineHelper myXPSPoly = new Company.XPStoPolyline.XPStoPolylineHelper(); //init brushes
        List<Polyline> LocalList = myXPSPoly.ReadFileOutputPolylineList(file, CurrentConfig.ImportTolerance);


        return LocalList;
    }

    void PolyFromXPSComplete(List<Polyline> PolylistIn)
    {

        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
        {

                PolylineList.Clear(); //already initialized in UI thread
                PolylineList = new List<Polyline>(PolylistIn.Capacity);

                foreach (Polyline currentPolyline in PolylistIn)
                {
                    Polyline correctedPolyLine = new Polyline();
                    PointCollection myPointCollection = new PointCollection();
                    Point pointToAdd = currentPolyline.Points[0]; //"The calling thread cannot access this object because a different thread owns it."
                    myPointCollection.Add(pointToAdd);
                    for (int i = 1; i < currentPolyline.Points.Count; i++)
                    {
                        ....copy points
                    }

                    correctedPolyLine.Points = myPointCollection;
                    correctedPolyLine.Stroke = currentPolyline.Stroke;
                    correctedPolyLine.StrokeThickness = 1;
                    PolylineList.Add(correctedPolyLine);
                }

                //display for testing
                VectorGridCanvas.Children.Clear();
                foreach (Polyline myPolyLine in PolylineList)
                {
                    VectorGridCanvas.Children.Add(myPolyLine);
                }

        }));           
    }
  • 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-26T17:46:43+00:00Added an answer on May 26, 2026 at 5:46 pm

    Because Polyline is a WPF UIElement class, it is bound to the UI thread. If you try to access any of its properties from another thread, it will throw an exception.

    Be mindful that Polyline is a display class, not a data type. You shouldn’t be fiddling with WPF controls or other display elements from background threads. WPF will fight you every step of the way.

    Your best bet is to perform your XPS to polyline conversion calculations using an intermediate data type of your own design. Anything but a UIElement will do. A value type struct might be appropriate. Once you’ve done whatever intensive computational gymnastics warrant a background thread, spit out your data and signal the UI thread that the data is good to go. The UI thread can then read the intermediate data and construct Polylines as needed on the UI thread.

    If you have tens of millions of polylines to construct and observe an objectionable UI hiccup, you could break the work into smaller batches to keep from blocking the UI for too long at a time.

    Polyline (and WPF in general) is bound to the UI thread, and no amount of background threading is going to change that.

    Don’t assume threading will save you.

    First, do you actually have a demonstrable UI blocking issue? If not, you’re done. No threading needed.

    If you do have a UI blocking issue, can it be broken up into smaller batches and still execute on the UI thread? If yes, you’re done. No threading needed.

    If using a background thread really is the most effective way to solve your problem, then implement it without using WPF classes like Polyline.

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

Sidebar

Related Questions

i have list of rows that user select and i want to delete them,
I have list of catalog paths and need to filter out some of them.
I have list of objects that contain information about which classes static EventHandler should
I have list of items in my activity which I load dynamically, item after
I have list of categories that is contained in li 's on one column
I have List<Product[]> and I need to join them into one Product[] .
i have list of items IList with data that looks list this: GenId TestMode
I have list of Guid's List<Guid> MyList; I need to copy its contents to
I have List collection that is populated in specific order (the requirement is that,
I have list of rows that I have fetched using mysql_fetch_array() . Now I

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.