I have a list of download links I am using cefsharp to download. I can't use webclient in order to download the files, the user must be logged in.
Here is my code:
foreach (var lnk in collection) {
Console.WriteLine(lnk);
await TestAsync(lnk);
}
private Task TestAsync(string lnk) {
return Task.Run(() => TaskToDo(lnk));
}
private async void TaskToDo(string lnk) {
await wb.GetBrowser().MainFrame.EvaluateScriptAsync(String.Format("window.location.href = '{0}'", lnk));
}
In my loop, I have a Console.WriteLine to print the links. In my program output, it prints all the links, then it downloads a file. The problem is that it only downloads the last file. I need it to wait and download the next file.
How can I solve this?
Your problem is
async void
That will NOT block by the caller, which is why your doing a Task.Run when you don't need to.
foreach (var item in collection)
{
Console.WriteLine(lnk);
await TestAsync(lnk);
}
private Task TestAsync(string lnk)
{
return TaskToDo(lnk);
}
private Task TaskToDo(string lnk)
{
return wb.GetBrowser().MainFrame.EvaluateScriptAsync(String.Format("window.location.href = '{0}'", lnk));
}
You are not using await keyword properly. In this case, you are telling that you want to wait each time you iterate loop. It is better to return Task from TestAsync(lnk) and put on the task list.
So, instead await method in foreach loop you should use Task.WhenAll() for all tasks like in following example:
List<Task> taskList = new List<Task>();
foreach (var lnk in collection)
{
Console.WriteLine(lnk);
var task = TestAsync(lnk);
taskList.Add(task);
}
await Task.WhenAll(taskList.ToArray());
Regarding:
The problem is that it only downloads the last file. I need it to wait
and download the next file.
How can I solve this?
I suspect that you cannot download file with window.lotion.href asynchronously because you are changing the location of window sequentially and before the previous operation complete!
Basically, while you are in middle of downloading LINK1 another task, example LINK3 will start to download and LINK1 will be stopped. This is the reason why you have only one file downloaded. Try to use window.open() or different some different technique.
Your code should be async all the way and you should not block on async code. Please refer to the #Stephen Cleary's MSDN article for more information: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
The issue here is that your TestAsync method starts a new Task, which will be executed on a background thread, and returns immediately before the task has actually finisihed.
You should make the TestAsync method async and change the return type of the TaskToDo method to Task to be able to await this one:
foreach (var lnk in collection) {
Console.WriteLine(lnk);
await TestAsync(lnk);
}
private async Task TestAsync(string lnk)
{
await TaskToDo(lnk);
}
private async Task TaskToDo(string lnk)
{
await wb.GetBrowser().MainFrame.EvaluateScriptAsync(String.Format("window.location.href = '{0}'", lnk));
}
Related
This question already has answers here:
Running multiple async tasks and waiting for them all to complete
(10 answers)
Closed last month.
I want to run several tasks in async and wait for all of them to complete.
private static Task AddAFavicon(int id)
{
// some operation where each task create excel sheet as per id.
}
static void Main(string[] args)
{
// i have this main method which contain publications in list
XElement Publication = XElement.Load() // read publication from xml file.
foreach (var item in Publication.Descendants())
{
//here i want to call this AddAFavicon method for each item as async task
}
//wait for all task to complete
}
I want to wait for all tasks to complete and then do my other stuff.
thanks in advance :)
You can use the method Task.WhenAll to wait for multiple async tasks to complete:
Task t1 = DoSomethingAsync1();
Task t2 = DoSomethingAsync2();
await Task.WhenAll(t1, t2);
In your specific case you can do something like:
List<Task> tasks = new List<Task>();
foreach (var item in Publication.Descendants())
{
int id = 0; // TODO: Initialize properly.
tasks.Add(AddAFavicon(id));
}
await Task.WaitAll(tasks);
You can do await AddAFavIconAsync(); in your loop. It will wait for your current task to complete before going on the next item and doing the same action.
Note your code here is incomplete, an async function should have the async keyword in its declaration, if not you will not be able to await anything inside that method.
So you would have something more like :
private static async Task AddAFaviconAsync(int id)
{
// some operation where each task create excel sheet as per id.
}
public static async Task Main(string[] args)
{
// i have this main method which contain publications in list
XElement Publication = XElement.Load() // read publication from xml file.
foreach (var item in Publication.Descendants())
{
await AddAFavIconAsync(item.id);
//here i want to call this AddAFavicon method for each item as async task
}
}
You can also note that I added Async at the end of the method names. Its just a basic naming convention you should use :). Just not for the Main method (as shown here).
A class has async method MonitorAsync(), which starts a long-running parallel operation. I have a collection of these monitors; these are all kicked off as follows:
internal async Task RunAsync()
{
var tasks = monitors.Select((p) => p.Value.MonitorAsync());
await Task.WhenAll(tasks);
}
If a monitor falls over, I need to know (basically I will run it up again). I've looked into ContinueWith and so on but when running a bunch of async tasks in parallel, how can I ensure I definitely know when one ends?
For context, RunAsync is basically the core of my application.
If a monitor falls over, I need to know (basically I will run it up again).
The easiest way to do this is to define this logic in a separate method:
internal async Task RunAsync()
{
var tasks = monitors.Select(p => MonitorAndRestart(p));
await Task.WhenAll(tasks);
async Task MonitorAndRestart(P p)
{
while (true)
{
try { await p.Value.MonitorAsync(); }
catch { ... }
p.Restart();
}
}
}
If you want to know when one ends (and that does not affect the others), ContinueWith() could be the way.
Alternatively, how about WaitAny in a loop?
while(anyTaskUnfinished){
await Task.WaitAny(tasks);
}
//Stuff you do after WhenAll() comes here
I am uncertain if you have to remove already finished Tasks. Or if it waits for any newly finishing.
You can try this:
If you do not want to call the Task.Wait method to wait for a task's completion, you can also retrieve the AggregateException exception from the task's Exception property
internal async Task RunAsync()
{
var tasks = monitors.Select((p) => p.Value.MonitorAsync());
try
{
await Task.WhenAll(tasks);
}
catch (Exception)
{
foreach (var task in tasks.Where(x => x.IsFaulted))
foreach (var exception in task.Exception.InnerExceptions)
{
// Do Something
}
}
}
Reference: Exception handling (Task Parallel Library)
I want to call an asynchronous method multiple times in a xUnit test and wait for all calls to complete before I continue execution. I read that I can use Task.WhenAll() and Task.WaitAll() for precisely this scenario. For some reason however, the code is deadlocking.
[Fact]
public async Task GetLdapEntries_ReturnsLdapEntries()
{
var ldapEntries = _fixture.CreateMany<LdapEntryDto>(2).ToList();
var creationTasks = new List<Task>();
foreach (var led in ldapEntries)
{
var task = _attributesServiceClient.CreateLdapEntry(led);
task.Start();
creationTasks.Add(task);
}
Task.WaitAll(creationTasks.ToArray()); //<-- deadlock(?) here
//await Task.WhenAll(creationTasks);
var result = await _ldapAccess.GetLdapEntries();
result.Should().BeEquivalentTo(ldapEntries);
}
public async Task<LdapEntryDto> CreateLdapEntry(LdapEntryDto ldapEntryDto)
{
using (var creationResponse = await _httpClient.PostAsJsonAsync<LdapEntryDto>("", ldapEntryDto))
{
if (creationResponse.StatusCode == HttpStatusCode.Created)
{
return await creationResponse.Content.ReadAsAsync<LdapEntryDto>();
}
throw await buildException(creationResponse);
}
}
The system under test is a wrapper around an HttpClient that calls a web service, awaits the response, and possibly awaits reading the response's content that is finally deserialized and returned.
When I change the foreach part in the test to the following (ie, don't use Task.WhenAll() / WaitAll()), the code is running without a deadlock:
foreach (var led in ldapEntries)
{
await _attributesServiceClient.CreateLdapEntry(led);
}
What exactly is happening?
EDIT: While this question has been marked as duplicate, I don't see how the linked question relates to this one. The code examples in the link all use .Result which, as far as I understand, blocks the execution until the task has finished. In contrast, Task.WhenAll() returns a task that can be awaited and that finishes when all tasks have finished. So why is awaiting Task.WhenAll() deadlocking?
The code you posted cannot possibly have the behavior described. The first call to Task.Start would throw an InvalidOperationException, failing the test.
I read that I can use Task.WhenAll() and Task.WaitAll() for precisely this scenario.
No; to asynchronously wait on multiple tasks, you must use Task.WhenAll, not Task.WaitAll.
Example:
[Fact]
public async Task GetLdapEntries_ReturnsLdapEntries()
{
var ldapEntries = new List<int> { 0, 1 };
var creationTasks = new List<Task>();
foreach (var led in ldapEntries)
{
var task = CreateLdapEntry(led);
creationTasks.Add(task);
}
await Task.WhenAll(creationTasks);
}
public async Task<string> CreateLdapEntry(int ldapEntryDto)
{
await Task.Delay(500);
return "";
}
Task.WaitAll() will deadlock simply because it blocks the current thread while the tasks are not finished (and since you are using async/await and not threads, all of your tasks are running on the same thread, and you are not letting your awaited tasks to go back to the calling point because the thread they are running in -the same one where you called Task.WaitAll()-, is blocked).
Not sure why WhenAll is also deadlocking for you here though, it definitely shouldn't.
PS: you don't need to call Start on tasks returned by an async method: they are "hot" (already started) already upon creation
I'm still getting up to speed with async & multi threading. I'm trying to monitor when the Task I Start is still running (to show in a UI). However it's indicating that it is RanToCompletion earlier than I want, when it hits an await, even when I consider its Status as still Running.
Here is the sample I'm doing. It all seems to be centred around the await's. When it hits an await, it is then marked as RanToCompletion.
I want to keep track of the main Task which starts it all, in a way which indicates to me that it is still running all the way to the end and only RanToCompletion when it is all done, including the repo call and the WhenAll.
How can I change this to get the feedback I want about the tskProdSeeding task status?
My Console application Main method calls this:
Task tskProdSeeding;
tskProdSeeding = Task.Factory.StartNew(SeedingProd, _cts.Token);
Which the runs this:
private async void SeedingProd(object state)
{
var token = (CancellationToken)state;
while (!token.IsCancellationRequested)
{
int totalSeeded = 0;
var codesToSeed = await _myRepository.All().ToListAsync(token);
await Task.WhenAll(Task.Run(async () =>
{
foreach (var code in codesToSeed)
{
if (!token.IsCancellationRequested)
{
try
{
int seedCountByCode = await _myManager.SeedDataFromLive(code);
totalSeeded += seedCountByCode;
}
catch (Exception ex)
{
_logger.InfoFormat(ex.ToString());
}
}
}
}, token));
Thread.Sleep(30000);
}
}
If you use async void the outer task can't tell when the task is finished, you need to use async Task instead.
Second, once you do switch to async Task, Task.Factory.StartNew can't handle functions that return a Task, you need to switch to Task.Run(
tskProdSeeding = Task.Run(() => SeedingProd(_cts.Token), _cts.Token);
Once you do both of those changes you will be able to await or do a .Wait() on tskProdSeeding and it will properly wait till all the work is done before continuing.
Please read "Async/Await - Best Practices in Asynchronous Programming" to learn more about not doing async void.
Please read "StartNew is Dangerous" to learn more about why you should not be using StartNew the way you are using it.
P.S. In SeedingProd you should switch it to use await Task.Delay(30000); insetad of Thread.Sleep(30000);, you will then not tie up a thread while it waits. If you do this you likely could drop the
tskProdSeeding = Task.Run(() => SeedingProd(_cts.Token), _cts.Token);
and just make it
tskProdSeeding = SeedingProd(_cts.Token);
because the function no-longer has a blocking call inside of it.
I'm not convinced that you need a second thread (Task.Run or StartNew) at all. It looks like the bulk of the work is I/O-bound and if you're doing it asynchronously and using Task.Delay instead of Thread.Sleep, then there is no thread consumed by those operations and your UI shouldn't freeze. The first thing anyone new to async needs to understand is that it's not the same thing as multithreading. The latter is all about consuming more threads, the former is all about consuming fewer. Focus on eliminating the blocking and you shouldn't need a second thread.
As others have noted, SeedingProd needs to return a Task, not void, so you can observe its completion. I believe your method can be reduced to this:
private async Task SeedingProd(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
int totalSeeded = 0;
var codesToSeed = await _myRepository.All().ToListAsync(token);
foreach (var code in codesToSeed)
{
if (token.IsCancellationRequested)
return;
try
{
int seedCountByCode = await _myManager.SeedDataFromLive(code);
totalSeeded += seedCountByCode;
}
catch (Exception ex)
{
_logger.InfoFormat(ex.ToString());
}
}
await Task.Dealy(30000);
}
}
Then simply call the method, without awaiting it, and you'll have your task.
Task mainTask = SeedingProd(token);
When you specify async on a method, it compiles into a state machine with a Task, so SeedingProd does not run synchronously, but acts as a Task even if returns void. So when you call Task.Factory.StartNew(SeedingProd) you start a task that kick off another task - that's why the first one finishes immediately before the second one. All you have to do is add the Task return parameter instead of void:
private async Task SeedingProdAsync(CancellationToken ct)
{
...
}
and call it as simply as this:
Task tskProdSeeding = SeedingProdAsync(_cts.Token);
I have two tasks which download an mp3 from web address inside local storage and returns timeSpan.
public async Task<TimeSpan> filedownload_Ignition_Outside()
{
Uri sourceIgnition_Outside = new Uri(TemporaryAddress.download_playCarSound+"Ignition_Outside");
//download and store file
return duration_Ignition_Outside;
}
public async Task<TimeSpan> filedownload_Idle_Outside()
{
Uri sourceIdle_Outside = new Uri(TemporaryAddress.download_playCarSound +"Idle_Outside");
StorageFile destinationFileIdle_Outside;
//download and store file
return duration_Idle_Outside;
}
Now I need to show an indeterminate progressbar in UI while downloading starts till it ends But dont know how to find the task completed?
On my NavigatedTo function I have set it as async and am calling
await downloadFile();
Now inside my downloadFile()
public async Task<TimeSpan> downloadFiles()
{
//ProgressShow
var temp= await filedownload_Ignition_Outside();
//if (filedownload_Ignition_Outside().IsCompleted)
//{
// //progressStop
//}
return temp;
}
But its not executing the stop statement as it waits asynchronously how can I get the event where both task gets finished? Can I call both tasks inside my downloadFiles() method and still able to get an event for both task completion.
Try something similar to the code given below in your OnNavigatedTo event.
ShowProgress=true;
downloadFiles().ContinueWith((sender) =>
{
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
ShowProgress=false;
);
});
You need to run the ShowProgress=False; in UI thread