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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:02:45+00:00 2026-05-26T02:02:45+00:00

I have a window that is subscribing to an event that is raised by

  • 0

I have a window that is subscribing to an event that is raised by an object. I’m subscribing to the event as follows:

m_Monitor.NewJob += new Monitor.NewJobEvent(NewJobEventHandler);

The event is being raised successfully and is calling my NewJobEventHandler just fine. The problem occurs when I try to make changes to any UI elements in the window. I get the follow exception:

System.Reflection.TargetInvocationException: {“The calling thread
cannot access this object because a different thread owns it.”}

I’ve had this issue before and I’ve used some “Dispatcher” with some “Action” to get around it. I’ll admit that whole thing seem like magic to me and I really didn’t understand it, but it did get me past the issue. Coming from a C++ background I’m a bit miffed that I can’t access certain things because they are in different threads. But anyways, my NewJobEventHandler() routine is as follows:

private void NewJobEventHandler(JobInfo newJob)
{
    TreeViewItem tvItem = new TreeViewItem();
    tvItem.Header = "Job: " + newJob.ToString();

    //The following call is what causes the exception, yet I've used very
    //similar code to update the .Text property of a TextBox from another
    //thread
    OutputDataTree.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (Action)(() => { OutputDataTree.Items.Add(tvItem); }));
}

Anyone know why this doesn’t work for me?

  • 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-26T02:02:45+00:00Added an answer on May 26, 2026 at 2:02 am

    You are creating the TreeViewItem in the worker thread, but use it in the UI thread. You’ll have to create it in the Action.

    private void NewJobEventHandler(JobInfo newJob)
    {
        TreeViewItem tvItem = new TreeViewItem();
        string header = "Job: " + newJob.ToString();
    
        //The following call is what causes the exception, yet I've used very
        //similar code to update the .Text property of a TextBox from another
        //thread
        OutputDataTree.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (Action)(() => { 
                                      TreeViewItem tvItem = new TreeViewItem();
                                      tvItem.Header = header;
                                      OutputDataTree.Items.Add(tvItem); 
                                   }));
    }
    

    This is of concern for any UI element you create, they need to be created in the UI thread to be useable.

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

Sidebar

Related Questions

I have a window that uses a viewmodel. This screen contains 2 listviews on
I have a window that contains one QTMovieView. How do I make it so
I have a window that I sometimes open using Show() and sometimes using ShowDialog()
I have a window that is databound to a ViewModel. The ViewModel has many
I have a window that contains several rather complex views. Right now, I'm using
So I'm trying to have a window that I can drag around by the
I'm running into a problem where, I have a Window that contains a child
I have a modal window that uploads files to server. Works great. Upon completion
I have a custom window (that should be on top of everything, including the
I have an iframe window that displays a report. To access this iframe, 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.