Question

I am wanting to check to make sure that I am invoking methods that return void when a method only returns Task and not Task<T>.

I've got a helper method that it's signature looks like this:

internal async static Task DrawLeafMidrail(Graphics dc, float totalHeightFeet,    List<HorizontalContainer> horizontals, bool showDetails, bool showVerticalDetails)
        {
            await Task.Run(() =>
            {
                float y, sl, startX, endX, stileWidth = 0, btmRailPx = 0, leftJmb;
                PointF[] points;
                List<VerticalContainer> verticals = new List<VerticalContainer>();
                List<LineContainer> lines = new List<LineContainer>();

                //..code omitted
            });
        }

And I am calling this helper method of Task like:

Horizontal.DrawLeafMidrail(
                           dc, doorPoints.BayRect.Height / Constants.PIXELS_PER_FOOT,
                           lstHors, ,forDoorDetail).Wait();    

Notice that I've got .Wait() appended to the end of the caller. I just want to make sure that this is the correct way and this will have the same effect as if the caller was to call the helper method like:

Task t = Horizontal.DrawLeafMidrail(dc, doorPoints.BayRect.Height / Constants.PIXELS_PER_FOOT, lstHors, i == 0, forDoorDetail);
t.Wait();
Was it helpful?

Solution

I am wanting to check to make sure that I am invoking methods that return void when a method only returns Task and not Task.

This part of the question doesn't make sense.

I just want to make sure that this is the correct way and this will have the same effect

Will it have the same effect? Yes. Task does not change expression evaluation, operator precedence, or how local variables behave.

Is it correct? No. You should be using await. Otherwise, you could deadlock, as I explain in my MSDN article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top