【发布时间】:2011-10-06 19:07:22
【问题描述】:
我之前遇到过这种问题,通过自定义类引发的事件更新 GUI 元素,但我能够通过以下方式解决它:
MyTextBlock.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(Action)(() => { LoggingTextBlock.Text += Message += "\r\n"; }));
我认为这个问题是 UI 线程独有的,但显然它适用于所有线程。不幸的是,我的自定义类没有像 UIElements 那样调用的 .Dispatcher() 例程。那么你应该如何将对象传递给其他线程并让它们能够使用它们呢?
例如,我有一个基础侦听器类,其主要工作基本上是查找一些数据、引发事件并传递该数据。以下是我要传递的数据类的一部分:
// 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; }
}
}
以下是类的一部分,它在线程中运行、收集数据并触发事件以将该数据传递给任何收听的人。
// 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);
}
}
}
以下是实例化 JobMonitor 类并捕获其事件的类的一部分。它无法访问 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();
}
}
所以 JobProcessor.NewJobEventHandler() 方法是我得到异常“调用线程无法访问此对象,因为不同的线程拥有它”的地方。我需要能够访问此属性以及下面的属性和方法。我需要做什么才能访问我需要的东西?
【问题讨论】:
标签: c# multithreading events parameter-passing