0

I am new to C# and now I am trying to learn async and await by coming up with my own example. But I am having trouble implementing it. Basically I stole this example from MSDN: https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx

But in the main method, instead of what is there, I put:

static void Main(string[] args)
    {

        Person[] pArray = new Person[100];

        //populate first 50 people
          //TODO: USE ASYNC
        //populate last 50 people
          //TODO: USE ASYNC
        //await here
        People peopleList = new People(pArray);

        //do some linq
        Console.ReadKey();

    }

I don't really know how to implement the TODO part (I am not sure if this is how you to it either). Should I create a task method that is parallel to main? But the pArray won't be global in this way and the task function shouldn't be able to modify the variable, right? I was wondering how you could implement this. Any tips will be appreciated!

Thanks!

Hanming Zeng
  • 337
  • 1
  • 4
  • 12
  • 2
    I'm affraid this is not a good approach to learn how to use `await` (or more generally, concurrency/asynchronny/parallelism). First, you want to avoid any shared state (in this case, `pArray`) - instead, you'd ideally make each of the async methods return their own parts of the data, and you'd just put them back together on the main thread. Second, you need to ensure `await` has somewhere to return - in your code, `Main` will simply end and kill everything as soon as you hit the first `await`. Try www.albahari.com/threading/ for a primer :) – Luaan May 26 '16 at 14:51
  • I might be wrong, but array is not thread-safe.. So you should start with thread-safe collection first. Then you can move-on filling it up by threads as you wish. Or in each thread use the save instance of object to LOCK the pArray on manipulations with it. Otherwise deadly multithreading world wouldn't mercy you. ;) * thread safe collections: https://msdn.microsoft.com/en-us/library/dd997305(v=vs.110).aspx How to lock: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx – Anatolyevich May 26 '16 at 14:54
  • @Luaan what if i change it to pArray1 and and pArray2, 50 length each? i want them to populate at the same time so later i might be able to await and do some linq on those two Arrays? – Hanming Zeng May 26 '16 at 15:01
  • 3
    @Anatolyevich provided each thread is working on a separate part of the array, it is thread safe. See [this related question](http://stackoverflow.com/questions/1460634/are-c-sharp-arrays-thread-safe/1460660). – Charles Mager May 26 '16 at 15:09

1 Answers1

1

It's far easier to learn async/await if you start by making I/O-bound operations asynchronous. Just creating People and adding them to an array is CPU-bound, not I/O-bound.

For async, you want to start with I/O. For example, you may want to download Google's homepage:

var client = new HttpClient();
var result = await client.GetStringAsync("http://www.google.com/");

Also, learning await is easier with a UI app rather than a Console app. So stick the code above in a button click event and see what happens.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810