Classe Semaphore e Thread.CurrentThread.ManagedThreadId

domenica 11 luglio 2010 - 00.11

cosmopsis Profilo | Junior Member

Sto iniziando a studiare il threading. Per la classe Semaphore ho questo codice di esempio:

static void Main(string[] args) {
Semaphore mySemaphore = null;
const int kMaxNum = 5;

try {
mySemaphore = Semaphore.OpenExisting("MYSEMAPHORE");
} catch (WaitHandleCannotBeOpenedException) {
//impossibile aprire perché non esiste
if (mySemaphore == null){
mySemaphore = new Semaphore(0, 4, "MYSEMAPHORE");
for (int i = 1; i <= kMaxNum; i++) {
Thread t = new Thread(new ParameterizedThreadStart(exampleFunction));
t.Start(mySemaphore);
}

Thread.Sleep(500);
mySemaphore.Release(kMaxNum - 1);
}
}
}

static void exampleFunction(object i) {
Console.WriteLine("{0} - {1}", Thread.CurrentThread.ManagedThreadId, i);
}

L'output è:

3 - System.Threading.Semaphore <===
4 - System.Threading.Semaphore
5 - System.Threading.Semaphore
6 - System.Threading.Semaphore
7 - System.Threading.Semaphore

Mi spieghereste perchè il ManagedThreadId parte sempre da 3?
grazie
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5