2010-02-12 23 views
5

Ho questo codice in C#:discussioni attivando C#

Thread t1 = new Thread(functionsActivations(3, 4000, 0, 4)); 
Thread t2 = new Thread(functionsActivations(3, 4000, 5, 9)); 
t1.start(); 
t2.Start(); 
Thread t3 = new Thread(functionsActivations(4, 4000, 0, 4)); 
Thread t4 = new Thread(functionsActivations(4, 4000, 5, 9)); 

Non sta funzionando. Come posso dire di chiamare il metodo che ho dato? In secondo luogo, voglio che t3 e t4 siano attivati ​​dopo che t1 e t2 finiscono. Come lo posso fare? In terzo luogo, voglio che t1 e t2 non si blocchino (in modo che t2 non debba attendere fino al termine di t1). È quello che ho fatto giusto?

+0

oh, non sapevo che ... – aharon

+2

Yea mate, 2/11 Accettato non va bene.Passa attraverso queste domande e segna quelle che hanno risposto alla tua situazione come accettate. –

risposta

4

"Non funziona" non è un insieme di sintomi molto chiaro. Cosa stai osservando?

EDIT: Ok, ora che hai detto che cos'è l'errore del compilatore, è molto più facile da diagnosticare. Al momento stai chiamando un metodo e provi a utilizzare il risultato come attività da eseguire per il thread. Supponendo che si realtà vuole fare quella chiamata di metodo quando viene avviato il filo, si desidera qualcosa di simile:

C# 2:

Thread t1 = new Thread(delegate() { functionsActivations(3, 4000, 0, 4); }); 

C# 3:

Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4)); 

Un altnerative a avere espressioni lambda dappertutto sarebbe scrivere un metodo di utilità:

private static Action DeferFunctionActivations(int a, int b, int c, int d) 
{ 
    return() => functionsActivations(a, b, d, d); 
} 

allora si potrebbe utilizzare:

Thread t1 = new Thread(DeferFunctionActivations(3, 4000, 0, 4)); 

ecc

Per il resto del post darò per scontato C# 3.

Inoltre, t1.start() dovrebbe essere t1.Start() - C# è case sensitive.

Per rispondere al punto finale, t1 e t2 sono attualmente indipendenti: non si bloccheranno a vicenda a meno che non ci sia la sincronizzazione da qualche parte nel codice che stanno eseguendo.

Se desideri solo t3 e t4 cominciare quando t1 e t2 hanno terminato, è possibile utilizzare Thread.Join:

Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4)); 
Thread t2 = new Thread(() => functionsActivations(3, 4000, 5, 9)); 
t1.Start(); 
t2.Start(); 
t1.Join(); 
t2.Join(); 
Thread t3 = new Thread(() => functionsActivations(4, 4000, 0, 4)); 
Thread t4 = new Thread(() => functionsActivations(4, 4000, 5, 9)); 
t3.Start(); 
t4.Start(); 

Si noti che questo significa che questa discussione aspetterà fino t1 e t2 hanno terminato, pure. Se ciò non è abbastanza buono per te, ci sono varie opzioni ma fondamentalmente vorrai qualcos'altro per attendere in modo asincrono il completamento di t1 e t2. Ad esempio, si potrebbe legare un filo in più per farlo:

Thread t1 = new Thread(() => functionsActivations(3, 4000, 0, 4)); 
Thread t2 = new Thread(() => functionsActivations(3, 4000, 5, 9)); 
t1.Start(); 
t2.Start(); 
Thread t3 = new Thread(() => functionsActivations(4, 4000, 0, 4)); 
Thread t4 = new Thread(() => functionsActivations(4, 4000, 5, 9)); 
Thread t5 = new Thread(() => 
{ 
    t1.Join(); 
    t2.Join(); 
    t3.Start(); 
    t4.Start(); 
}); 
t5.Start(); 

Un po 'icky, ma dovrebbe funzionare.

Sei in grado di utilizzare .NET 4.0? In tal caso, il framework Parallel Extensions rende molto più semplice questo.

+0

grazie! mi hai risolto le ultime due domande, ma non la prima. non funziona per altri motivi, il problema è nella funzione che ho dato al thread in the ctor. mi dice che non può convertire da void a system.threading.threadstart ... – aharon

+0

Perché stai passando un costruttore in un thread? Cosa stai cercando di fare? Creare un oggetto in un thread perché la costruzione richiede molto tempo? – Dave

+0

no, questo codice è nel ctor, passo le normali funzioni al thread – aharon