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?
I would look into the SyncronizationContext provided by System.Threading. This class has a static property
Currentthat 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
PrintSystemJobInfoto 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).