views:

338

answers:

2

I'm using the AsyncOperation class to avoid having to write tons of "if (control.InvokeRequired) then/else" methods (as opposed to its traditional role in the Event-Based Asynchronous Pattern). In some cases, I don't really care about getting a notification when the worker thread is complete. Because of this, I'd like to not call the PostOperationCompleted method on my AsyncOperation, since it would require me to write a do-nothing callback.

I'm curious whether it's a good idea to omit the call to PostOperationCompleted. The documentation indicates that it ends the lifetime of the operation and makes further calls invalid, It does not make it clear whether there is internal behavior associated with this call that is vital. It does somewhat hint that there might be internal consequences for omitting the call, but in the end I don't see any strong recommendations to always call it.

So, should I consider it bad practice to omit the call, or is it no big deal?

A: 

The documentation for PostOperationCompleted states "The AsyncOperation object will ensure that your delegate is invoked on the thread or context appropriate for the application model". This is very important if your GUI thread needs to do something once a background operation has completed. I think this is probably the most important reason to use this class in the first place!

Cheers, Dan :)

Dan
Right, this is why I explicitly stated my UI doesn't care.
OwenP
Ok. Well I guess I'm saying that you don't even need to use this class if you don't care when a worker thread completes. The purpose of AsyncOperation is to track the lifetime of a background op. If you don't need to do this, just queue an operation on a thread pool thread and forget about it
Dan
Mind you...I don't really know the specifics of your project so maybe my comments don't really apply :)
Dan
...Yet another comment. Perhaps your in your code you don't know whether you need to post or not until you do the background op? You could just call OperationCompleted instead of PostOperationCompleted if you don't need to post to the GUI thread.
Dan
A: 

Owen,

If you know that in your situation you are not requiring the marshalling back onto the original callers thread, then you should just call OperationCompleted. There is an implict simple state machine held between your operation and its underlying SynchronizationContext. That in itself will not be a problem if you know that once your operation has ceased, it is not used from the rest of your application. While this is ok actually, from a programmatic perspective, it has the following things to consider:

  1. First of all, someone coming to the code with the experience of how to use this construct will be expecting some type of completion of the async operation, so it would be confusing - you should make this clear in the code.

  2. A key aspect of the completion of the operation is that you remove the object from the finalizer list. For one or two instances this might not be a problem, but you would certainly take a performance hit if the number of these operations is large since you would be allowing all of this AsyncOperation instances to get promoted and the GC has to do more work. So you should at least call OperationCompleted to avoid this.

  3. Only call the Post version if you really need to because again you use up resources, here in the name of an extra job submitted to the threadpool, which later involves context switches and handling of the job. If not required avoid it, especially if point 2 is valid...though for good practice its best to call the appropriate version.

HTH

Nick.

Nick Robinson