1
votes

I am getting the warning "This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. Although it sounds obvious, I thought I was covered because I run all the processes and have an Await Task.WhenAll() in my processFiles method. Do I need to be doing this a different way? Any thoughts would be kindly appreciated.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;


    namespace ProcessFiles
    {
        public partial class ProcessFiles : Form 
        {
            public ProcessFiles(int userLevel, int userID)
            {
            }

            private void btnProcess_Click(object sender, EventArgs e)
            {
                processFiles();

            }

            async Task<int> processFiles()
            {
                var processingTasks = new List<Task>();
                foreach (string fileName in listBox1.Items)
                {
                    processingTasks.Add(process012(fileName));
                    processingTasks.Add(process123(fileName));
                    processingTasks.Add(process234(fileName));

                }
                await Task.WhenAll(processingTasks);
                return (1);
            }


            async Task<int> process173(string fileName)
            {
                return (retVal);

            }

            async Task<int> process032(string fileName)
            {
                return (retVal);
            }

            async Task<int> process018(string fileName)
            {
                return (retVal);
            }
1
You have 3 methods with the async keyword but no await. Isn't the message clear? Also, if you want to return a completed task use return Task.FromResult without asyncPanagiotis Kanavos
I thought await Task.WhenAll(processingTasks); was sufficient to wait for everything to complete. The last three methods don't have an await in them.Missy
they have an async which means they need an await. And so does btnProcess_ClickPanagiotis Kanavos
Okay - thanks very muchMissy

1 Answers

2
votes

No need to use async Task in your processXXX methods just return Task<int> instead. You will await the tasks in main processFiles() method:

        private async void btnProcess_Click(object sender, EventArgs e)
        {
            await processFiles();
        }

        async Task<int> processFiles()
        {
            var processingTasks = new List<Task>();
            foreach (string fileName in listBox1.Items)
            {
                processingTasks.Add(process012(fileName));
                processingTasks.Add(process123(fileName));
                processingTasks.Add(process234(fileName));

            }
            await Task.WhenAll(processingTasks);
            return (1);
        }


        Task<int> process173(string fileName)
        {
            return Task.FromResult(retVal)
        }

        Task<int> process032(string fileName)
        {
            return Task.FromResult(retVal)
        }

        Task<int> process018(string fileName)
        {
           return Task.FromResult(retVal)
        }