Quantcast
Channel: async / await / task
Viewing all articles
Browse latest Browse all 19

async / await / task

$
0
0

Hi Markus Freitag:

1) This sample given by you isn't complete, and for full runnable sample, please download this WPF version instead at:https://docs.microsoft.com/zh-cn/samples/dotnet/samples/async-and-await-cs/

2) Let's turn to your questions:

await Task.Run(() => operations.Example1Async(personArgs1, progress));

This means you will asynchronously wait for the result of Task when the task itself finishes its own work, your screen such as UI WON'T be blocked.

@Kareninstructor:Sorry but just a reminder——according to your codes at "Example1Async",I think you don't need to use await for the two tasks, because you have "Task.WaitAll(taskOne, taskTwo);", which means both of the async methods are running until they all finish their own work.

 public async Task Example1Async(PersonArguments personArguments, IProgress<string> progress)
        {
            Task<Person> taskOne = Task.Factory.StartNew(async () =>
            {
                var person = new Person() { Id = -1 };

                var lines = File.ReadAllLines(personArguments.FileName);
                for (var index = 0; index < lines.Length - 1; index++)
                {
                    var lineParts = lines[index].Split(',');
                    if (lineParts[1] == personArguments.FirstName && lineParts[2] == personArguments.LastName)
                    {
                        person.Id = Convert.ToInt32(lineParts[0]);
                        person.Birthday = DateTime.Parse(lineParts[4]);
                        if (lineParts[3] == "1" || lineParts[3] == "2")
                        {
                            person.Gender = lineParts[3] == "1" ? "Female" : "Male";
                        }
                        progress.Report($"Id is {lineParts[0]}");
                        break;
                    }

                    await Task.Delay(1, _cancellationToken);
                }

                progress.Report("Task 1 complete");
                return person;

            }, _cancellationToken);

            var taskTwo = Task.Factory.StartNew(async () =>
            {
                for (var index = 0; index < 5; index++)
                {
                    _cancellationToken.ThrowIfCancellationRequested();
                    progress.Report(index.ToString());

                    await Task.Delay(12, _cancellationToken);

                    _cancellationToken.WaitHandle.WaitOne(500);
                }

                progress.Report("Task 2 complete");
                return true;

            }, _cancellationToken);

            Task.WaitAll(taskOne, taskTwo);
            progress.Report("Both task done!");


Reproduce your quesions with ScreenToGif is your choice. 
For IIS: IIS Forum
For WebSite of .NET: ASP.NET Forum
For others: StackExchange
For spam-sender or forum urgent issues, Send your Email at:  forumsfeedback@microsoft.com




Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>