2

I have multiple REST API calls which I want to do in parallel way. E.g.

I have a REST API call CreateData(). I want to create 1000 datas in server which is adapted for multi-thread operation in 4-5 parallel threads.

I am C++ developer and know how multithreading works in C++. I am quite naive in C# coding. I know about async tasks but dont have idea how to acheive the above said task.

var response = await Task.Run(()=>CreateData());

Right now I am doing it in sequence where one by one I am creating the data in a single thread once I have recieved response of previous CreateData(). It is impacting the perfomance badly and taking hell lot of time.

I want to create say 5 threads which will do the REST API calls in parallel manner for all the 1000 users.

2 Answers2

5

Right now you are assigning response to the result of the Task.Run which also requires the Task to complete in some regard. This happens automatically when compiled because you are using the keyword await.

If you prefer, you can just assign response to the running Task itself and keep going. Of course I wouldn't call it response anymore. Let's say you do that and call it task1 instead.

var task1 = Task.Run(()=>CreateData());

Now your code will continue running and task1 will just represent the running Task.

If you had 5 you could do them all like it sounds that you want.

var task1 = Task.Run(()=>CreateData());
var task2 = Task.Run(()=>CreateData());
var task3 = Task.Run(()=>CreateData());
var task4 = Task.Run(()=>CreateData());
var task5 = Task.Run(()=>CreateData());

Now you can also wait for all of these tasks to complete at the same time with Task.WhenAll method.

await Task.WhenAll(task1, task2, task3, task4, task5);

So to sum it up.

The await keyword does some compiler magic and basically puts a callback in that place of the method (assigning the rest of the method to be a continuation when the Task is complete) and ALSO assigns the Result of that Task to the variable (if the Task has a result). There is a lot to unpack here; I don't believe a short answer really justifies what's happening.

Without using the await keyword then you simply assign the Task itself to the variable.

Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46
  • 1
    @Md.ParwezAkhtar I did make a huge mistake in my examples; which I fixed just now. I told you not to use ```await``` if you wanted to assign the ```Task``` itself but in the examples I was using ```await```; which wouldn't have worked prior to my edit. – Michael Puckett II Jan 28 '19 at 05:40
2

You can refer to this https://stackoverflow.com/a/25010220/4209924

OR

You can do something like this.

    public async Task CreateData()
    {
        // your logic here with await
    }

    public async Task GetData()
    {
        var data1 = CreateData();
        var data2 = CreateData();
        var data3 = CreateData();
        var data4 = CreateData();
        await Task.WhenAll(new Task[] { data1, data2, data3, data4 });
    }
Syed Abdul Aala
  • 167
  • 1
  • 9