Простите, что на английском. Просто копирую уже сформулированный вопрос на этот форум.
I'm trying to write a C# application which needs a fresh info from internet. The program consists of the main AI part and many small modules. Each module is an instance of a class, which has a method with this signature:
Код:
MyResultClass Worker(MyArgument)
I want the program to work as follows:
- Load a Module (from DLL or create a new instance)
- Add it to a List<MyModule> loadedModules and start asynchronously executing its Worker method
- As a Worker finishes its task, the main program asynchronously processes its result and starts it again
Thus the AI can always assume that it has the newest possible in information.
The question is, what is the most natural way of implementing this algorithm? I've been googling for several hours with no result.
I tried to do the following
Код:
public void AddModule(MyModuleClass instance) {
//Add Module to List etc..
Task<MyResultClass> newTask = new Task<MyResultClass>(() => { return instance.Worker(obj); }); AddTask(newTask);
}
public void AddTask(Task<MyResultClass> t) {
var nextTask =t.ContinueWith((tfin) => { Task.Delay(100); AddTask(t); }); t.Start();
}
However, each task can be executed only once. When I call it the second time, the programs throws an exception that I can't run already executed task twice.