Is AsyncOperation.PostOperationCompleted required?

I'm using the class AsyncOperation

to avoid having to write tons of if (control.InvokeRequired) then / else methods (as opposed to its traditional role in an event-based asynchronous pattern). In some cases, it is not important for me to be notified when a workflow is complete. Because of this, I would not want to call the PostOperationCompleted

method PostOperationCompleted

on mine AsyncOperation

, since I would need to write a do-nothing callback.

I'm wondering if it is worth omitting the call PostOperationCompleted

. The documentation states that it ends the lifetime of the operation and invalidates the additional calls. It does not make it clear whether the internal behavior associated with this challenge is vital. He hints somewhat that there might be internal implications for rejecting a call, but at the end I don't see any strong recommendation to always call him.

So, should I consider it bad practice to omit the call, or doesn't it really matter?

0


source to share


2 answers


Owen,

If you know that in your situation you do not need to marshall back to the original caller thread, you should simply call OperationCompleted. There is a trivial state machine between your activity and its underlying SynchronizationContext. This in itself won't be a problem if you know that once your operation has stopped it won't be used from the rest of your application. While this is actually normal, from a programmatic point of view, the following should be considered:

  • First of all, someone coming into the code with experience with this construct will expect some sort of async operation to complete, so it would be confusing - you have to make it clear in your code.

  • The key aspect of completing an operation is removing the object from the finalizer list. For one or two instances, this might not be a problem, but you will certainly get a performance hit if the number of these operations is large, since you would let all of these AsyncOperation instances get promoted and the GC has to do more work. Therefore, you should at least call OperationCompleted to avoid this.

  • Only call the Post version if you really need to, because you are using resources again, here, in the name of additional work posted to the threadpool, which later includes context switches and job handling. If not required, avoid it, especially if point 2 is valid ... although for good practice it is best to name the appropriate version.



NTN

Nick.

+1


source


Documentation for PostOperationCompleted states "The AsyncOperation object ensures that your delegate is invoked on a thread or context appropriate for the application model." This is very important if your GUI thread needs to do something after the background operation completes. I think this is probably the most important reason for using this class in the first place!



Cheers, Dan :)

0


source







All Articles