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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:02:42+00:00 2026-05-31T22:02:42+00:00

I’m working on a project about PDF rendering in the C# language. I convert

  • 0

I’m working on a project about PDF rendering in the C# language. I convert each page of PDF file to Image and Adds it to a ObservableCollection with a new thread by the below code:

  ThreadStart myThreadDelegate = new ThreadStart(DoWork);
  myThread = new Thread(myThreadDelegate);
  myThread.SetApartmentState(ApartmentState.STA);

  void DoWork()
    {
        for (int i = 0; i < pdfFile.Pages.Count; i++)
        {
            PdfPage page=pdfFile.LoadPage(i);
            myObservableCollection[i]=page;
        }
    }

then pass the custom item of myObservableCollection to another UserControl for render it but I got an exception:

The calling thread cannot access this object because a different
thread owns it.

I know if I use UI thread my problem could be solved but I want load pdf pages in the background and user doesn’t wait for loading all pages and this is possible with a new thread.

  • 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-31T22:02:43+00:00Added an answer on May 31, 2026 at 10:02 pm

    You can use threads but have to use the Dispatcher to access UI elements. Only the part, where you pass the item to the UserControl has to be done by the dispatcher.

    Application.Current.Dispatcher.BeginInvoke(new Action(() => AddItem()));
    

    BeginInvoke is a asynchronous call and won’t block the execution of the following code.

    Edit: I’m still not 100% sure if I unterstood the whole idea of your application but made a small sample which demonstrates how you can use threads and UI elements.

    I made a Window (that would be your UserControl) which contains a Button and a ListBox. When clicking the Button a thread is started and processes some items. In my case it just adds some texts into a list, I added Thread.Sleep(1000) to simulate the processing of lots of stuff. When the text is prepared, it will be added to the ObservableCollection, which has to be done by the UI thread (Dispatcher). There is nothing blocking the UI but this adding and this is done very fast. You can also start multiple threads at the same time.

    This is the code-behind of the Window (the Window itsself just contains a Button and a ListBox):

    public partial class MainWindow : Window
    {
        private ObservableCollection<string> textList;
    
        public MainWindow()
        {
            textList = new ObservableCollection<string>();
            InitializeComponent();
            btnStartWork.Click += BtnStartWorkClick;
            lstTextList.ItemsSource = textList;
        }
    
        private void BtnStartWorkClick(object sender, RoutedEventArgs e)
        {
            Thread myThread;
            ThreadStart myThreadDelegate = DoWork;
            myThread = new Thread(myThreadDelegate);
            myThread.SetApartmentState(ApartmentState.STA);
            myThread.Start();
        }
    
        private void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                string text = string.Format("Text {0}", i);
                // block the thread (but not the UI)
                Thread.Sleep(1000);
                // use the dispatcher to add the item to the list, which will block the UI, but just for a very short time
                Application.Current.Dispatcher.BeginInvoke(new Action(() => textList.Add(text)));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.