This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Used to determine if Dispose()



Download 21,46 Mb.
Pdf ko'rish
bet539/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   535   536   537   538   539   540   541   542   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Used to determine if Dispose()

// has already been called.

private bool disposed = false;

public void Dispose()

{

// Call our helper method.



// Specifying "true" signifies that

// the object user triggered the cleanup.

CleanUp(true);

C H A P T E R   8  

U N D E R S TA N D I N G   O B J E C T   L I F E T I M E



263

8849CH08.qxd  10/22/07  1:27 PM  Page 263




// Now suppress finalization.

GC.SuppressFinalize(this);

}

private void CleanUp(bool disposing)



{

// Be sure we have not already been disposed!

if (!this.disposed)

{

// If disposing equals true, dispose all

// managed resources.

if (disposing)

{

// Dispose managed resources.

}

// Clean up unmanaged resources here.

}

disposed = true;



}

~MyResourceWrapper()

{

// Call our helper method.

// Specifying "false" signifies that

// the GC triggered the cleanup.

CleanUp(false);

}

}

Notice that MyResourceWrapper now defines a private helper method named CleanUp(). When



specifying true as an argument, we are signifying that the object user has initiated the cleanup,

therefore we should clean up all managed 



and unmanaged resources. However, when the garbage

collector initiates the cleanup, we specify false when calling CleanUp() to ensure that internal dis-

posable objects are

not disposed (as we can’t assume they are still in memory!). Last but not least,

our bool member variable (disposed) is set to true before exiting CleanUp() to ensure that Dispose()

can be called numerous times without error.

To test our final iteration of MyResourceWrapper, add a call to Console.Beep() within the scope

of your finalizer:

~MyResourceWrapper()

{

Console.Beep();



// Call our helper method.

// Specifying "false" signifies that

// the GC triggered the cleanup.

CleanUp(false);

}

Next, update Main() as follows:



static void Main(string[] args)

{

Console.WriteLine("***** Dispose() / Destructor Combo Platter *****");




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   535   536   537   538   539   540   541   542   ...   1830




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish