运行异步代码 C# 时未找到 AsyncMethodBuilder.cs

Sas*_*shi 6 .net c# asynchronous async-await c#-5.0

每当我尝试调试代码但无法继续时,我都会收到错误。

未找到 AsyncMethodBuilder.cs

背景信息:

我必须用最少的时间压缩大量的文档。我有一个压缩库,我能够同步进行压缩。但为了加快速度,我想并行压缩文档(一次压缩的文档没有可编程的),这样 1.我会正确利用 CPU 并在最佳时间内完成工作。

为了实现上述目标,我按照链接创建了多个任务并等待它们完成

下面是我尝试过的代码。

public static class CompressAllTechniques
    {
        public static void Main()
        {
            GoCallAPIAsync();
        }

        private static async void GoCallAPIAsync()
        {
            try
            {
                List<Task> TaskList = new List<Task>();
// For Sample testing I have taken 4 files and will iterate through them to check the timings
                const string originalFile1 = @"Sample Data\source";
                const string originalFile2 = @"Sample Data\source1";
                const string originalFile3 = @"Sample Data\Original";
                const string originalFile4 = @"Sample Data\Original1";
                List<string> arr = new List<string>() { originalFile1, originalFile2, originalFile3, originalFile4 };
                int noofThreads = 2;       // No of tasks that has to run paralally
                int IterationsCompleted = 0;
                int TotalIterations = 10;
                var temp = arr;
                Console.WriteLine("\n\nProcess Started @ " + DateTime.Now);
                for (int i = 0; i < TotalIterations; i++)
                {
                    if (temp.Count == 0) temp = arr;
                    var sd = temp.Take(noofThreads);

                    foreach (var item in sd)
                    {
                        var LastTask = new Task(() => GoCompress(item, IterationsCompleted));
                        LastTask.Start();
                        TaskList.Add(LastTask);
                    }
                    temp = temp.Except(sd).ToList();

                    await Task.WhenAll(TaskList.ToArray());
                    TaskList.Clear();
                }
                Console.WriteLine("\n\nProcess Completed @ " + DateTime.Now);
                Console.Read();
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        private static void GoCompress(string zdf, int dcnk)
        {
              // Compression Logic
        }        
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试调试上述代码时,我在 Visual Studio 中收到以下异常。

Locating source for 'f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs'. (No checksum.)
The file 'f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs' does not exist.
Looking in script documents for 'f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs'...
Looking in the projects for 'f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs'.
The file was not found in a project.
Run Code Online (Sandbox Code Playgroud)

Dan*_*nM7 6

就像 @stuartd 在上面的评论中所说的那样,启用(选中该框)调试选项“仅启用我的代码”:

启用调试选项启用“仅我的代码”

我从谷歌来到这里,虽然这似乎没有帮助@Sashi,但它解决了我的机器上的这个错误。