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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:05:47+00:00 2026-05-31T03:05:47+00:00

I’m having one thread that generates GUI elements in wpf. A canvas is there

  • 0

I’m having one thread that generates GUI elements in wpf. A canvas is there to draw objects (rectangles and so on …)

This wpf thread calls another thread let’s name it the calculation thread. This thread calculates the size and position and so on of the elements to be shown in the canvas.

I want to have these two parts (GUI and calculatio) run in different threads. The “calculation thread” is based on a library without references to wpf functionality.

Now I want to show intermediate data of the calculation thread displayed by the wpf thread.
I’m doing it that way:

The calc-thread fires an event (DataReady) that is implemented by the wpf-thread:

void MyRegStringObject_DataReady()
{
  if (DebugMode)
    MyDrawingBoard.DrawRegElements();
}

The problem now is, that an error is thrown: “calling thread cannot access this object because a different thread owns it”

There are some answered question here in stackoverflow referring to this error but none of them can help in my case.

The function DrawRegElements() wants to clear the canvas object (among other things):

curCanvas.Children.Clear();

At this position in the code the error is thrown.
It seems that the function MyRegStringObject_DataReady triggered by an event from the calc-thread is owened by the calc-thread also. But it is defined in class that the wpf-thread is based on.

How can I solve this problem? Does anyone have any idea?
By the way:
The calc-thread is called this way:

CalcElements = Task.Factory.StartNew<bool>(MyRegStringObject.CalcRegElements);

When the thread is finished I defined:

CalcElements.ContinueWith((FinishCalcRegElements) =>
{
  MyDrawingBoard.DrawRegElements();
}, CancellationToken.None, TaskContinuationOptions.None, 
TaskScheduler.FromCurrentSynchronizationContext());

No problem with that. Everythins runs perfect. The function defined in ContinueWith seems to be owened by the wpf-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-31T03:05:48+00:00Added an answer on May 31, 2026 at 3:05 am

    Error message is straightforward, you are trying access an UI element from a now owning thread. Delegate this work to WPF Dispatcher which is associated with Main UI thread, which would post all messages from worker thread to UI thread:

    Application.Current.Dispatcher.BeginInvoke((ThreadStart)delegate
       {            
           MyDrawingBoard.DrawRegElements();   
       });
    

    To be sure that you are using correct Dispatcher, associated with the Main UI thread, you can pass it as a parameter to the worker thread code, so just pass Dispatcher.Current from Main UI thread, otherwise calling Dispatcher.CurrentDispatcher in the worker thread will initialize a new instance associated with a calling worker thread. Or just use Application.Current.Dispatcher which would reference the Main UI thread’s Dispatcher.

    PS:

    But it is defined in class that the wpf-thread is based on

    This is wrong assumption, classes itself are not related to any thread. Concrtete class instances are. So you can create UI/non-UI related classes either in UI or worker thread.

    Also important point in which thread DataReady event has been fired?

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.