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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:57:53+00:00 2026-05-26T04:57:53+00:00

I’ve had this kind of issue before, with updating GUI elements via events raised

  • 0

I’ve had this kind of issue before, with updating GUI elements via events raised from custom classes, but I was able to get around it with with something like:

MyTextBlock.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
      (Action)(() => { LoggingTextBlock.Text += Message += "\r\n"; }));

I thought this problem was unique to UI threads, but apparently it applies for all threads. And unfortunately, my custom classes don’t have the .Dispatcher() routine to call like the UIElements have. So how are you supposed to pass objects to other threads and have them be able to use them?

For example, I have a base listener class whose main job is basically to find some data, raise an event, and pass that data. The following is a segment of the data class that I’d like to pass:

// Just the class for carrying the job data.
public class JobData
{
    // NOTE: I don't no have trouble accessing these properties from a 
    // different thread
    public string SomeProperty1 { get; set; }
    public string SomeProperty2 { get; set; }
    public string SomeProperty3 { get; set; }

    // ...
    // more properties
    // ...

    // This a .NET object of type System.Printing.PrintSystemJobInfo.  This
    // is the guy that gives me trouble.  Later on, I can't access members 
    // of Job without getting the "The calling thread cannot access this 
    // object because a different thread owns it" error.
    PrintSystemJobInfo m_Job = null;
    public PrintSystemJobInfo Job
    {
        get
        {
            if (m_Job == null)
            { throw new ArgumentNullException(); }

            return m_Job;
        }

        set { m_Job = value; }
    }
}

The following is a segment of the class that runs in a thread, gathers data, and fires events to pass that data off to anyone listening.

// Thread who monitors looking for data to package into JobData and send
// to any listeners.
public class JobMonitor
{
    public delegate void NewJobEvent(JobData jobStuff);
    public event NewJobEvent NewJob;

    // Call this when you have some job data and need to notify listeners
    private void OnNewJob(object newJobData)
    {
        JobData newJobData = (JobData)newJobData;
        if (NewJob != null)
        {
            NewJob(newJobData);
        }
    }

    private void WorkerRoutine()
    {
        while(true)
        {
            // Wait for data

            // ...
            // ...

            // when data is found

            JobData myJobData = new JobData();
            myJobData.SomeProperty1 = "some data";
            myJobData.SomeProperty2 = "some data";

            int jobID = GetCurrentJobID();
            string printerName = GetCurrentPrinterName();

            // .NET class System.Printing.PrintQueue
            PrintQueue printQueue = new PrintQueue(new PrintServer(), printerName);

            PrintSystemJobInfo jobInfo = null;
            jobInfo = printQueue.GetJob(jobID);

            // This is the guy in my JobData class that I'll have trouble accessing
            // later on.
            myJobData.Job = jobInfo;

            // Time to fire off the event
            OnNewJob(myJobData);
        }
    }
}

The following is a segment of the class that instantiates the JobMonitor class and captures its events. It has trouble accessing one of the properties for JobMonitor.

// This class signs up to recieve and process events from the JobMonitor class.
public class JobProcessor
{
    // this is the method that handles the incoming job events.  This is where 
    // i have trouble.
    private void NewJobEventHandler(JobData newJob)
    {
        string temp = newJob.SomeProperty1; // this works fine

        bool bTemp = newJob.Job.IsPaused;   // this throws an exception "The
                                            // calling read cannot access this
                                            // object because a different thread
                                            // owns it"
    }

    private JobMonitor m_monitor = null;
    public JobProcessor()
    {
        m_monitor = new JobMonitor();

        //attaches a method to handle incoming job events.
        m_monitor.NewJob += new JobMonitor.NewJobEvent(NewJobEventHandler);
        m_monitor.Start();
    }
}

So the JobProcessor.NewJobEventHandler() method is where I’m getting the exception “The calling thread cannot access this object because a different thread owns it”. I need to be able to access this property and the properties and methods underneath. What do I need to do to be able to access what I need?

  • 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-26T04:57:54+00:00Added an answer on May 26, 2026 at 4:57 am

    I would look into the SyncronizationContext provided by System.Threading. This class has a static property Current that will provide the Dispatcher style invoking that you are looking for.

    Other Possibilities

    If you want to handle the events on a different thread than what creates your JobData class, then you may need to move the actual retrieval of the PrintSystemJobInfo to the event handler.

    Another alternative would be to abstract away the System class with a new one you create (that doesn’t have thread affinity).

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from

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.