Results 1 to 3 of 3

Thread: Any way to simplify this code by using Func, Action and lambdas?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Any way to simplify this code by using Func, Action and lambdas?

    Hi!

    I need to write a piece of code that has to be executed in turn, e.g. first A, then B, then C then D. The methods have to be called async, but the flow of events is sync. I solved this by adding:

    - a delegate called HandleDataDelegate
    - an event called HandleData

    The class that does the work has four methods A, B, C, D all with different work but same signature void (string, string) when they are ready they raise an event handleData passing the result as a string

    HandleData("this is my result")

    In the "main" method I have methods OnADone, OnBDone, OnCDone, OnDDone that kick off a call to B, C or D. In the onDDone Everything is finished and the program ends. And to Control which of the eventhandlers that will get the result I do Before I call the method B, I add a handler to it's handleData event so that OnBDone will be used. And in OnBDone, I remove the handler and set the handler to OnCDone Before I call C and so on.


    This feels like a very clumpsy way to solve the problem, but it works and I haven't found any better way to handle this, since the methods A-D have an async call in them. The platform I work on doesn't support await, async and Tasks...

    Can anyone please help me to "streamline" the flow described, it feels like there is work to be done, for example can't I use the func<> to dynamically Control which method to be called? It would sure save some lines of code...

    /S

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Any way to simplify this code by using Func, Action and lambdas?

    It sounds like it would be possible to have a single method called something like OnDone that is called when ANY of the methods completes. An integer variable could be used to indicate which of the four methods was being executed, and OnDone would branch on that to call the next one in sequence.
    My usual boring signature: Nothing

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Any way to simplify this code by using Func, Action and lambdas?

    Is there any code inside the A(), B(), etc methods other than the asynchronous method call? For example, could B() be the callback supplied to the async call in A()?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width