リクエスト キューからのリクエストに基づいて作業を行うコマンド オブジェクトがあります。この特定のコマンドは、子 appdomain でその作業を実行します。子 appdomain での作業の一部には、ConcurrentQueue 操作 (Add や Take など) のブロックが含まれます。リクエスト キューを介して子アプリケーション ドメインにアボート シグナルを伝達し、その中のワーカー スレッドをウェイクアップできる必要があります。
したがって、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 が Token getter プロパティにアクセスしようとすると、例外が発生します。
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.
私の質問は、.NET 同時実行データ構造 (CancellationToken 引数がサポートされている場所) でブロックされる可能性のあるアプリケーション ドメインとウェイクアップ スレッド間で中止/キャンセル シグナルを伝達するにはどうすればよいかということです。