之前我遇到过这样的问题,通过自定义类引发的事件更新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()方法是我得到异常的地方“调用线程无法访问此对象,因为另一个线程拥有它”。我需要能够访问此属性以及下面的属性和方法。为了能够访问我需要的东西,我需要做什么?
答案 0 :(得分:2)
我会查看System.Threading提供的SyncronizationContext。这个类有一个静态属性Current
,它将提供你正在寻找的Dispatcher样式调用。
其他可能性
如果要在与创建JobData类不同的线程上处理事件,则可能需要将PrintSystemJobInfo
的实际检索移动到事件处理程序。
另一个替代方法是使用您创建的新类(没有线程关联)抽象出System类。