I am porting a program that had a very long download time, which was not improved at all when I ported it, because slowness was associated with access to the db server (and not to the non-optimal code). I moved the download to a separate thread using the library Tasks, and now the user interface does not freeze at boot, but I was interested to know something:
The object Task.Factoryhas a method StartNewthat should create a new task, start it and return a link to it. This makes sense, but it seems like the constructor is Taskdoing the same.
When I do the following:
Task catsFromDB = new Task(() => AddCategoriesFromDB(cts.Token), cts.Token);
catsFromDB.Start();
I am getting an error InvalidOperationExceptionthat I really don't understand. Looks like I already threw him away. Which I don’t have.
[EDIT]
. erorr: "InvalidOperationException," Start "cannot be called for a task that completed"
[EDIT]
The error is the cancellation code that I included. I needed to reset cancel before trying to start it again.
source
share