Mes connaissances multi-threads sont encore assez rudimentaires, donc j'apprécierais vraiment quelques pointeurs ici. J'ai une interface, IOperationInvoker (de WCF) qui a les méthodes suivantes:Opérations asynchrones au sein d'une opération asynchrone
IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
Compte tenu d'une mise en œuvre concrète de cette interface, je dois implémenter la même interface, tout en appelant la mise en œuvre sous-jacente dans une discussion séparée. (dans le cas où vous vous demandez pourquoi, l'implmentation concrète appelle un objet COM hérité qui doit être dans un état d'appartement différent).
En ce moment, je fais quelque chose comme ceci:
public StaOperationSyncInvoker : IOperationInvoker {
IOperationInvoker _innerInvoker;
public StaOperationSyncInvoker(IOperationInvoker invoker) {
this._innerInvoker = invoker;
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
{
Thread t = new Thread(BeginInvokeDelegate);
InvokeDelegateArgs ida = new InvokeDelegateArgs(_innerInvoker, instance, inputs, callback, state);
t.SetApartmentState(ApartmentState.STA);
t.Start(ida);
// would do t.Join() if doing syncronously
// how to wait to get IAsyncResult?
return ida.AsyncResult;
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
// how to call invoke end on the
// thread? could we have wrapped IAsyncResult
// to get a reference here?
return null;
}
private class InvokeDelegateArgs {
public InvokeDelegateArgs(IOperationInvoker invoker, object instance, object[] inputs, AsyncCallback callback, object state)
{
this.Invoker = invoker;
this.Instance = instance;
this.Inputs = inputs;
this.Callback = callback;
this.State = state;
}
public IOperationInvoker Invoker { get; private set; }
public object Instance { get; private set; }
public AsyncCallback Callback { get; private set; }
public IAsyncResult AsyncResult { get; set; }
public Object[] Inputs { get; private set; }
public Object State { get; private set; }
}
private static void BeginInvokeDelegate(object data)
{
InvokeDelegateArgs ida = (InvokeDelegateArgs)data;
ida.AsyncResult = ida.Invoker.InvokeBegin(ida.Instance, ida.Inputs, ida.Callback, ida.State);
}
}
Je pense que je dois conclure le AsyncResult retourné avec mon propre, donc je peux revenir au fil que nous avons bobiné ... mais honnêtement, je suis un peu hors de ma profondeur. Des pointeurs?
Un grand merci,
James
Merci beaucoup Barry - Je vais essayer! –