简体   繁体   English

C#并行性问题

[英]C# Parallelism Woes

So, I have been trying to study for Exam 70-483 in C#, because I need to get my certifications fairly desperately. 因此,我一直在努力学习C#考试70-483,因为我急需获得认证。

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." 我或多或少地在问我的“ WriteXMLAsync”和“ 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." 因此,让所有等待都在“ 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. 对于我正在寻找的信息,我将参考他的评论。

WriteXMLAsync should be declared like this, 应该这样声明WriteXMLAsync

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.) 注意: async方法仅在实现事件处理程序时才应返回void ,否则绝不会返回(尽管它将编译)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM