P. 1
Curs C# romana

Curs C# romana

4.75

|Views: 24,824|Likes:
Published by Luna Romantica
o carte care te invata de la inceput programarea in C#
o carte care te invata de la inceput programarea in C#

More info:

Published by: Luna Romantica on Aug 01, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

01/02/2016

pdf

text

original

Relativ la proprietatea boolean˘a IsBackground, trebuie f˘acut˘a precizarea

c˘a un fir de execut¸ie poate s˘a se execute ˆın fundal (background) sau ˆın prim

plan (foreground). Diferent¸a dintre cele dou˘a posibilit˘at¸i o constituie faptul

c˘a dac˘a un proces are m˘acar un fir de execut¸ieˆın foreground, CLR va ment¸ine

aplicat¸ia ˆın execut¸ie. O dat˘a ce toate firele de execut¸ie de tip foreground

se termin˘a, CLR va executa Abort() pentru fiecare fir de execut¸ie de tip

background (dac˘a mai exist˘a a¸sa ceva) ¸si termin˘a procesul.

Exemplu:

using System;

using System.Threading;

class Test

{

static void Main()

214

CURS 10. FIRE DE EXECUT¸IE

{

BackgroundTest shortTest = new BackgroundTest(10);

Thread foregroundThread =

new Thread(new ThreadStart(shortTest.RunLoop));

foregroundThread.Name = "ForegroundThread";

BackgroundTest longTest = new BackgroundTest(50);

Thread backgroundThread =

new Thread(new ThreadStart(longTest.RunLoop));

backgroundThread.Name = "BackgroundThread";

backgroundThread.IsBackground = true;

foregroundThread.Start();

backgroundThread.Start();

}

}

class BackgroundTest

{

int maxIterations;

public BackgroundTest(int maxIterations)

{

this.maxIterations = maxIterations;

}

public void RunLoop()

{

String threadName = Thread.CurrentThread.Name;

for(int i = 0; i < maxIterations; i++)

{

Console.WriteLine("{0} count: {1}",

threadName, i.ToString());

Thread.Sleep(250);

}

Console.WriteLine("{0} finished counting.", threadName);

}

}

Firul din foreground va ment¸ine procesul ˆın execut¸ie pˆan˘a cˆand se termin˘a

10.2. SINCRONIZAREA

215

ciclul s˘au while. Cˆand acesta se termin˘a, procesul este oprit, chiar dac˘a

ciclul while din firul de execut¸ie din background nu ¸si-a terminat execut¸ia.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->