How to write a custom task return method correctly

To run the code asynchronously (e.g. using async / await) I need the right task. Of course, there are several predefined methods in the structure that cover the most common operations, but sometimes I want to write my own. I am new to C #, so it is possible that Im doing it wrong, but Im at least not completely satisfied with my current practice. See the following example of what I am doing:

public async Task<bool> doHeavyWork()
    {
        var b1 = await this.Foo();
        //var b2 = await Task<bool>.Factory.StartNew(Bar); //using Task.Run
        var b2 =  await Task.Run(()=>Bar());
        return b1 & b2;
    }

public Task<bool> Foo()
    {
        return Task.Factory.StartNew(() =>
                                  {
                                      //do a lot of work without awaiting
                                      //any other Task
                                      return false;
                                  });
    }

public bool Bar()
    {
        //do a lot of work without awaiting any other task
        return false;
    }

, Foo, " , , imho. - , , Bar, , , , async ( , BarAsync), Task.Factory.StartNew . , , " , , , , .

, : ? " " ( , )?

Servy, . , ( ).

+5
1

, - Bar, /? , , , ? , , , ASP ? , , , Task.

, , Bar , , , Task.

, .NET Task.Factory.StartNew Task.Run, (tad).

+3

All Articles