0

So, I have been trying to study for Exam 70-483 in C#, because I need to get my certifications fairly desperately.

However, Parallelism has just caused me a huge headache. I understand the basic idea, but a lot of code I've been reading implies the assumption that only async methods should used in async methods.

More or less, I'm asking what is the difference between My "WriteXMLAsync" and "WriteXMLAsyncTwo."

If I'm not mistaken, both are run in parallel to the main thread. So, whats the point of having all the awaits on the "WriteXMLAsync." It doesn't seem to be justified in anyway as it'll do the same thing as the second method if I'm right.

namespace TaskProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Task TaskOne = Task.Factory.StartNew(() => WriteXMLAsync());
            Task TaskTwo = Task.Factory.StartNew(() => WriteXMLAsyncTwo());

            List<Task> TaskList = new List<Task> { TaskOne, TaskTwo };

            Task.WaitAll(TaskList.ToArray());

            Console.WriteLine("Both Tasks Have Finished.");
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey(); 
        }

        static async void WriteXMLAsync()
        {
            try
            {
                XmlWriterSettings XMS = new XmlWriterSettings();
                XMS.Async = true;

                using (XmlWriter Writer = XmlWriter.Create("FileAsyncOne.xml", XMS))
                {
                    await Writer.WriteStartElementAsync("", "root", "");
                    await Writer.WriteEndElementAsync();
                    await Writer.FlushAsync();
                }
            }
            catch (Exception Ex)
            {
                //Not what we'd normally do, but hey its good enough for this example.
                Console.WriteLine("Encountered an Exception.");
            }
        }

        static void WriteXMLAsyncTwo()
        {
            try
            {
                using (XmlWriter Writer = XmlWriter.Create("FileAsyncTwo.xml"))
                {
                    Writer.WriteStartElement("root");
                    Writer.WriteEndElement();
                }
            }
            catch(Exception Ex)
            {
                Console.WriteLine("Encountered an Exception.");
            }
        }
    }
}

//Edit: Although, I have accepted Jodrell's answer. I would refer to his comment more for the information I was looking for.

  • 4
    Your question is unclear. What do you mean by "for them to actually be async"? Rather than just relying on labels, I suggest you show a short but complete program and ask detailed questions about where you're confused. – Jon Skeet Mar 12 '15 at 08:16
  • Maybe this article helps: http://stackoverflow.com/questions/15148852/async-await-vs-threads – arnie Mar 12 '15 at 08:25

1 Answers1

0

WriteXMLAsync should be declared like this,

static async Task WriteXMLAsync()
{
    ...
}

allowing you to do

Task.WhenAll(new []
    {
        WriteXMLAsync(),
        Task.Factory.StartNew(() => WriteXMLAsyncTwo())
    }).Wait();

Then, there is an obvious difference.

Note: async methods should only return void if they are implementing event handlers, never otherwise (although it will compile.)

Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • If I'm not mistaken, the only obvious difference is that changing the void to task allows it to be executed without doing all the lambda expression jazz. I'm more worried about the methods' internals. Why does WriteXMLAsync have all of those await calls? It seems pointless to my newbie eyes. –  Mar 12 '15 at 10:56
  • @ZacWade, In the context of your console app it is pointless. In a larger multi-threaded application the `await` will yield control of the thread, allowing the thread to be used for something else while the asynchronous operation is performed. You get the same result but the threads are managed differently while the function is performed. – Jodrell Mar 12 '15 at 11:03