How to use threading or Task pattern in following scenario

Dotnet Learner

I have two methods using in Main method like this

public bool isBadMethod() 
{ 
   bool isBad = Method1();
   if(isBad) return true; 
   else return Method2(); 
}

bool Method1() { ..... return true or false }         
bool Method2() { ..... return true or false }

If any of Method1 or Method2 returning true no need to check for other method we can return MainMethod as true. But if first method returns false then need to check for second method and finally return whatever second method returns. Here Method1 and Method2 are time consuming methods. Can I use asynchronous programming on these methods. Help how to do. (I'm using .net 4.0)

Adam Houldsworth

You could just make use of short-circuiting of expressions:

bool val = Method1() || Method2();

If Method1 returns true, it doesn't bother with the second method.

Obviously, if the method calls are expensive then continue with the task route, as this isn't parallel execution. I'm just posting this in case it's an XY Problem.


Using tasks, this might be something like what you want. It will run both methods and then just use an expression to grab the right result. In this instance it waits the full 3 seconds to grab the result of Method2. This also does cancellation. I am not sure if the types used are best practice as my experience with this is patchy, but it gets the job done in my sandbox:

internal class Program
{
    private static void Main(string[] args)
    {
        var cm2 = new CancellationTokenSource();

        var m1 = Task.Factory.StartNew(() => Method1());
        var m2 = Task.Factory.StartNew(() => Method2(cm2.Token), cm2.Token);

        var val = m1.Result || m2.Result;

        cm2.Cancel();

        Console.WriteLine(val);
        Console.ReadLine();
    }

    private static bool Method1()
    {
        Thread.Sleep(1000);
        Console.WriteLine(1);
        return true;
    }

    private static bool Method2(CancellationToken token)
    {
        Thread.Sleep(3000);

        if (token.IsCancellationRequested)
            return false;

        Console.WriteLine(2);
        return true;
    }
}

As Matthew Watson points out in the comments, the cancellation token needs to be used by Method2 in order to actually "cancel". In my example, it simply stops the printing of the number and returns early.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use threading or Task pattern in following scenario

From Dev

What pattern to use for the following scenario?

From Dev

What pattern to use for the following scenario?

From Dev

How to use RethinkDB indices in the following scenario?

From Dev

How to use expandable list view in the following scenario

From Dev

Design pattern to handle following scenario

From Dev

How to use create event on a jQuery dialog in following scenario?

From Dev

How to access json data and use in if condition in following scenario?

From Dev

How can I use list comprehension in following scenario?

From Dev

How to manipulate the array in following scenario?

From Dev

How to change the array in following scenario?

From Dev

How to get result in following scenario

From Dev

How to get result in following scenario

From Dev

How to deal with following scenario in QTP?

From Dev

How would I improve the performance in the following scenario

From Dev

How to show the groupwise result for the following scenario?

From Dev

How to make the checkbox working properly in following scenario?

From Dev

how to make a left outer join in following scenario

From Dev

How to avoid duplicates in following SQL scenario

From Dev

How to apply conditional aggregation in SQL the following scenario?

From Dev

How can I use regex pattern to replace this following string

From Dev

How To Use Threading on RxJava?

From Dev

How To Use Threading on RxJava?

From Dev

How to identify the most suitable Design Pattern for a scenario

From Dev

SSIS how to use a table created in a SQL Task as destination in a following Data Flow Task

From Dev

How to use CROSS APPLY in this scenario

From Dev

How to use collect() in drools in this scenario

From Dev

How to implement autocomplete functionality using PHP, jQuery and AJAX in following scenario?

From Dev

How to create an inner array inside parent array in following scenario?

Related Related

  1. 1

    How to use threading or Task pattern in following scenario

  2. 2

    What pattern to use for the following scenario?

  3. 3

    What pattern to use for the following scenario?

  4. 4

    How to use RethinkDB indices in the following scenario?

  5. 5

    How to use expandable list view in the following scenario

  6. 6

    Design pattern to handle following scenario

  7. 7

    How to use create event on a jQuery dialog in following scenario?

  8. 8

    How to access json data and use in if condition in following scenario?

  9. 9

    How can I use list comprehension in following scenario?

  10. 10

    How to manipulate the array in following scenario?

  11. 11

    How to change the array in following scenario?

  12. 12

    How to get result in following scenario

  13. 13

    How to get result in following scenario

  14. 14

    How to deal with following scenario in QTP?

  15. 15

    How would I improve the performance in the following scenario

  16. 16

    How to show the groupwise result for the following scenario?

  17. 17

    How to make the checkbox working properly in following scenario?

  18. 18

    how to make a left outer join in following scenario

  19. 19

    How to avoid duplicates in following SQL scenario

  20. 20

    How to apply conditional aggregation in SQL the following scenario?

  21. 21

    How can I use regex pattern to replace this following string

  22. 22

    How To Use Threading on RxJava?

  23. 23

    How To Use Threading on RxJava?

  24. 24

    How to identify the most suitable Design Pattern for a scenario

  25. 25

    SSIS how to use a table created in a SQL Task as destination in a following Data Flow Task

  26. 26

    How to use CROSS APPLY in this scenario

  27. 27

    How to use collect() in drools in this scenario

  28. 28

    How to implement autocomplete functionality using PHP, jQuery and AJAX in following scenario?

  29. 29

    How to create an inner array inside parent array in following scenario?

HotTag

Archive