question

Arsium-4135 avatar image
0 Votes"
Arsium-4135 asked Arsium-4135 commented

Asynchronous delegates optimization

Is there something I should do to optimize my delegates ?

         private delegate int DoOperation();
         private readonly DoOperation doOperation;
         public bool stopOperation {get;set;}
      
         internal OperationConstr() : base()
         {
             doOperation= new DoOperation (Operation);
         }
    
    
         internal void OperationAsync()
         {
             doOperation.BeginInvoke(new AsyncCallback(EndOperation), null);
         }
    
         private int Operation()
         {
             //big work here
             return 0;
         }
    
         private void EndOperation(IAsyncResult ar) 
         {
             int result = doOperation.EndInvoke(ar);
    
             if (!stopOperation )
                 OperationAsync();
             else
             {
                 this.Dispose();
                 return;
             }
         }
dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is there a problem with the current code?

1 Vote 1 ·
Arsium-4135 avatar image Arsium-4135 karenpayneoregon ·

Nope it's working fine just curious if it could be improved ?

0 Votes 0 ·

Maybe you should optimise the design considering async, await, etc.


0 Votes 0 ·

I don't like this approach of async working (prefer to use delegates instead of that)

0 Votes 0 ·

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered Arsium-4135 commented

Delegates by definition are going to be slower than regular code. If you have to use them then go ahead but accept the perf issues. You should profile the code. Most likely the delegate calls are not slow at all compared to everything else. Unless you can identify an actual bottleneck then there isn't much you can optimize.

Personally I think you should consider an alternative design where you don't need a delegate. Delegates were useful in the past (and still can be) but honestly most code has moved on to better solutions including passing function arguments, using events or async/await. Delegates are not designed to be a catch all solution and other approaches may prove to be faster.

Without further understanding what problem you're trying to solve using delegates it is hard to recommend better approaches.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't speak about speed (I mispoke) I mean "is my code correct for asynchronous delegates ?"? Do I have to use WaitHandle ? Or things like that to improve the code above.

Also I'm using delegates for async calls because I find them clearer than async/await with ugly Task.Run everywhere

0 Votes 0 ·

You shouldn't need WaitHandle unless you need to wait such that other threads need to block.

Also I'm using delegates for async calls because I find them clearer than async/await with ugly Task.Run everywhere

You shouldn't need to use Task.Run in most cases. If you have sync code then it runs synchronous otherwise you use await/async. The only use for Task.Run is to run sync code as async and that is generally the wrong answer. However your code is far more complex (hence your question) then simply calling Task.Run and it is going to negatively impact the code base in the long run. If all this code is just to avoid a call to Task.Run then you are creating a complex solution to a simple problem. Just use Task.Run.

Perhaps if you could provide us a simple example (code) of a use case where you actually need all this code then we can provide an easier way to make it async. To be honest I don't think you need all that code even to avoid Task.Run on a delegate but it depends on your use case.

1 Vote 1 ·

Ok ok just was curious if my code above could be improved. And yes I prefer delegates style with callbacks in my case.

Don't know too that Task.Run was not a bad approach of async/await.

0 Votes 0 ·