Is it safe to call the same method several times asynchronously in C# 5

I would be grateful if you could let me know opinion about calling asynchronously the same method several times. I more interested in know the safety side of this activities. I have posted here a console code that contain one method that I am calling asynchronously four times and it is working well. So far I have not noted any hiccups. But I want to be sure. Please the following code:

public class OneMethodCalledServeralTimes
    {
        protected async Task<bool> DoSomeWork(Values values, int whenItIsCalled, string description)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            await Task.Delay(values.Value);

            Console.WriteLine("{0} Completed in {1} and this was called: {2}",description, stopWatch.ElapsedMilliseconds, whenItIsCalled);

            return true;
        }

        public bool DoAllWork()
        {
            Console.WriteLine("=====================Started doing weekend Chores===================");
            var task1 = DoSomeWork(new Values { Value = 10000 }, 1, "First work to be done");
            var task2 = DoSomeWork(new Values {Value = 7000}, 1, "First work to be done");
            var task3 = DoSomeWork(new Values { Value = 4000 }, 2, "second work to be done");
            var task4 = DoSomeWork(new Values { Value = 1000 }, 3, "third work to be done");

            Task.WaitAll(task1, task2, task3, task4);

            Console.WriteLine("=====================Completed doing weekend Chores===================");

            return true;
        }

    }

The following is the Console application calling the above call:

static void Main(string[] args)
        {
            //var weekend = new HomeWork().DoAllWork();

            Console.WriteLine("############################Using proper methods#############################");


            var workToBeDone= new OneMethodCalledServeralTimes().DoAllWork(); //Passing parameters and the most successful one

            Console.WriteLine("It took the entire four mothod {0} seconds to finish", stopewach.ElapsedMilliseconds/1000.0);

            Console.ReadKey();
        }

I also highly welcome any concise views about the corns and pros of calling asynchronously the same method several times

Jon Skeet
people
quotationmark

It depends on the method - but it's certainly not inherently unsafe. There's nothing within the state machine generated by the compiler that introduces problems. However, if your asynchronous method uses shared state, then the normal caveats apply. For example, you wouldn't want to call this multiple threads concurrently:

static Task DoBadThingsAsync(List<string> list)
{
    await Task.Delay(500);
    list.Add("Hello!");
}

... because List<T> isn't safe to use in a multi-threaded environment if any of the threads perform writes (without synchronization).

One point to note is that if you've got asynchronous methods which are expected to be used in a "single thread synchronization context" (e.g. the UI thread in a WPF or WinForms app) then you don't need to worry about thread safety - but you do need to worry about general concurrency, as both invocations of the method could be "live" at the same time.

people

See more on this question at Stackoverflow