Syntaxe utilisation de délégués avec 2 paramètres
Bonjour,
Je souhaiterais utiliser la class PCQueue définit ici (http://www.albahari.com/threading/pa...Parallel_Class) mais avec un délégué Action à 2 paramètres
3 bloques de code :
- La class Producteur/consommateur
- La méthode consommé TraiterAction
- L'instentiation de PCQueue
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public class PCQueue : IDisposable
{
BlockingCollection<Action<string, string>> _taskQ = new BlockingCollection<Action<string, string>>();
public PCQueue (int workerCount)
{
// Create and start a separate Task for each consumer:
for (int i = 0; i < workerCount; i++)
{
Task.Factory.StartNew(Consume);
}
}
public void Dispose()
{
_taskQ.CompleteAdding();
}
public void EnqueueTask(Action<string, string> action)
{
_taskQ.Add(action);
}
void Consume()
{
// This sequence that we're enumerating will block when no elements
// are available and will end when CompleteAding is called.
foreach (Action<string, string> action in _taskQ.GetConsumingEnumerable())
{
action();//=> ERREUR : Perform action
}
}
} |
La méthode consommée
Code:
1 2 3 4 5
|
public static void TraiterAction(string txt1, string txt2)
{
Console.WriteLine(txt1 + " " + txt2);
} |
L'appel de la class producteur/consommateur pourrait s'écrire ainsi :
Code:
1 2 3 4 5
|
PCQueue pq = new PCQueue(1);
pq.EnqueueTask(TraiterAction("texte 1", "texte 2");//=> ERREUR
pq.EnqueueTask(TraiterAction("texte 3", "texte 4");//=> ERREUR
pq.Dispose(); |
Merci par avance pour la réponse !