2014 dxdy logo

Научный форум dxdy

Математика, Физика, Computer Science, Machine Learning, LaTeX, Механика и Техника, Химия,
Биология и Медицина, Экономика и Финансовая Математика, Гуманитарные науки




Начать новую тему Ответить на тему
 
 Multiple infinite asynchronous threads C#
Сообщение12.01.2014, 13:50 
Заслуженный участник
Аватара пользователя


30/10/10
1481
Ереван(3-й участок)
Простите, что на английском. Просто копирую уже сформулированный вопрос на этот форум.

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:

  1. Load a Module (from DLL or create a new instance)
  2. Add it to a List<MyModule> loadedModules and start asynchronously executing its Worker method
  3. 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.

 Профиль  
                  
Показать сообщения за:  Поле сортировки  
Начать новую тему Ответить на тему  [ 1 сообщение ] 

Модераторы: Karan, Toucan, PAV, maxal, Супермодераторы



Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group