我有一个命令对象,根据来自请求队列的请求进行工作。这个特定的命令将在一个子appdomain中执行它的工作。在儿童appdomain中完成其工作的一部分涉及对ConcurrentQueue操作(例如,添加或取出)的阻塞。我需要能够通过请求队列传播中止信号,传递给子appdomain,并唤醒其中的工作线程。如何通过AppDomain边界传递CancellationToken?
因此,我认为我需要跨AppDomain边界传递CancellationToken。
我试图创建一个类从MarshalByRefObject的继承:
protected class InterAppDomainAbort : MarshalByRefObject, IAbortControl
{
public InterAppDomainAbort(CancellationToken t)
{
Token = t;
}
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
return null;
}
public CancellationToken Token
{
get;
private set;
}
};
,并把该上的工人功能参数:
// cts is an instance variable which can be triggered by another thread in parent appdomain
cts = new CancellationTokenSource();
InterAppDomainAbort abortFlag = new InterAppDomainAbort(cts.Token);
objectInRemoteAppDomain = childDomain.CreateInstanceAndUnwrap(...);
// this call will block for a long while the work is being performed.
objectInRemoteAppDomain.DoWork(abortFlag);
但我仍然得到一个异常时objectInRemoteAppDomain尝试访问令牌获取器属性:
System.Runtime.Serialization.SerializationException: Type 'System.Threading.CancellationToken' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
我的问题是:我如何通过appdomains传播中止/取消信号,并唤醒可能在.NET并发数据结构(支持CancellationToken参数)中被阻塞的线程。
就我所知,这是完美的解决方案。 – usr 2013-03-02 22:38:53