| Programming | Software Engineering | Web Design | Database | Operating Systems

More info on 'using' and resource management

CyrusN
Keywords: using,resource management
From: http://blogs.msdn.com/cyrusn/archive/2005/05/10/416278.aspx

Joe Duffy (PM on the CLR team) posted to my blog about the whole using/resource management subject.  Being an cool and froody guy (who i may or may not have ever met) he linked back to a great post of his own on the subject *and* included even more information on the subject.  I'm going to repost his post here (since formatting of comments sucks on Community Server) so you can read it, and i also recommend you read his blog since it's chock full of good content.

--- (the following is written by Joe) ---

I cover this in fair detail in my article, which I was happy to notice somebody already linked to!

This is a problem we really want to solve in Orcas. I noted a couple possible solutions at the bottom of the article. I had a few other speculations, but after conversations w/ Chris Brumme, I realized most of them were crap and we'd never recommend people write their code that way (nevermind have C# spit the code).

For example,

ExpressionType temp = null; 
try { 
    try {
    } finally {
        temp = /*async-exception gets held up if triggered here*/ expression;
    } 
    /* ...and actually throws here */ 
    statement 
} finally { 
    if (temp != null) { 
        ((IDisposable)temp).Dispose(); 
    } 
}

will ensure that an async exception won't get raised, since we hold off on processing them during finally block execution. We also don't execute them while you're in a CER, running some opaque unmanaged code, and a few other corner scenarios. But holding up an async abort is a pretty rude thing to do, especially if you intend to block inside of it.

Further, you could imagine a class like

static class ReliableFactory { 
    public static void New<T>(out T created) where T : new() { 
        try {
        } finally { 
            created = new T(); 
        } 
    } 
} 

which would enable you to write

ExpressionType temp = null; 
try { 
    ReliableFactory.New<ExpressionType>(out temp); 
    /* similar to above, an async exception won't prevent the out assignment to temp */ 
    statement 
} finally { 
    if (temp != null) { 
        ((IDisposable)temp).Dispose(); 
    } 
} 

But this is fairly hokey.

I really hope we solve this problem in Orcas. But there's quite a bit to do with Dispose that needs a fixin', too. :)

 

 

 

 

---------------

 

What about something that took a delegate instead of a where T: new? Then you could use anonymous methods, and locals, etc.

Something like this:

public delegate T Creator<T>();

public static class ReliableFactory {
public static T Create<T>(Creator<T> create) {
try { }
finally {
return create();
}
}
}

class C {
void F() {
using (ReliableFactory.Create(delegate { expression here); })) {
// stuff.
}
}
}
 
 
----------------------------
How about:

public delegate T Creator<T>();

public static class ReliableFactory {
public static IDisposable Create<T>(out T temp, Creator<T> create) where T : IDisposable {
try { }
finally {
temp = create();
return null;
}
}
}

class C {
void F() {

using (IDisposable use = null, ignore = ReliableFactory.Create(out use, delegate { new LayoutTransaction(this, this, PropertyNames.Dock); })) {
// stuff.
}
}
}

Related Article
  • What’s the deal with the C# “using” construct
  • Tutorial for building J2EE Applications using JBOSS and ECLIPSE

  • Comment
    aricet with buspar Post At: 2006-2-2 13:54:01
    Hello, it's very good site too: soluability of tramadol , overdose tramadol http://yakormnevjopu.dynodns.net/tramadol/soluability_of_tramadol.html
    buy line vicodin Post At: 2006-2-15 21:38:37
    vladgondonpozornii
    cialis lavitra Post At: 2006-3-9 15:32:54
    Very good map: http://arhusa.biz/map0.html
    buy generic viagrra Post At: 2006-3-9 15:35:00
    Very good map: http://stohuevpodkilem.locationinformationservice.com/map0.html
    zzz Post At: 2006-12-8 12:38:54





    Tsuruda Post At: 2007-6-15 13:31:54
    hi all, nice site
    http://nokiaringtonesworld.info/free-nokia-ringtones/download-nokia-phone-ringtone.php http://nokiaringtonesworld.info/free-nokia-ringtones/cellular-south-nokia-cell-phone-free-ringtones.php http://nokiaringtonesworld.info/free-nokia-ringtones/cheap-ringtones-for-a-nokia-phone.php http://nokiaringtonesworld.info/free-nokia-ringtones/susan-and-the-surftones-tiki-kiki-nokia-ringtone.php http://nokiaringtonesworld.info/free-nokia-ringtones/ringtone-for-nokia-phone.php http://nokiaringtonesworld.info/free-nokia-ringtones/nokia-ringtones.php http://nokiaringtonesworld.info/free-nokia-ringtones/nokia-1100-ringtones.php http://nokiaringtonesworld.info/free-nokia-ringtones/nokia-2125i-ringtones-for-free.php http://nokiaringtonesworld.info/free-nokia-ringtones/the-breakaways-nokia-6651-ringtones.php http://nokiaringtonesworld.info/free-nokia-ringtones/free-ringtone-for-sprint-phone-nokia-3588i.php http://nokiaringtonesworld.info/free-nokia-ringtones/free-nokia-ringtones-mobile-phone.php http://nokiaringtonesworld.info/free-nokia-ringtones/free-ringtones-for-nokia-5100-series.php http://nokiaringtonesworld.info/free-nokia-ringtones/download-free-polyphonic-nokia-ringtones.php
    thecarspeakerwatch Post At: 2007-7-3 1:21:20
    The best links in internet
    http://google.com/translate_c?u=wshosting.biz/viagra/?page=245 viagara and cialis lead http://google.com/translate_c?u=wshosting.biz/viagra/?page=69 coumadin viagara http://google.com/translate_c?u=wshosting.biz/viagra/?page=189 puerta viara http://google.com/translate_c?u=wshosting.biz/viagra/?page=297 viagara pill cutter http://google.com/translate_c?u=wshosting.biz/viagra/?page=313 viagara use http://google.com/translate_c?u=wshosting.biz/viagra/?page=197 shelf life of viarga http://google.com/translate_c?u=wshosting.biz/viagra/?page=304 viagara stories http://google.com/translate_c?u=wshosting.biz/viagra/?page=227 vaigra on line http://google.com/translate_c?u=wshosting.biz/viagra/?page=328 viagras effect on women http://google.com/translate_c?u=wshosting.biz/viagra/?page=62 compare cialis viagara levitra http://google.com/translate_c?u=wshosting.biz/viagra/?page=131 increase blood flow using viagara http://google.com/translate_c?u=wshosting.biz/viagra/?page=294 viagara over 4 hours http://google.com/translate_c?u=wshosting.biz/viagra/?page=155 marsupial mandibular angular process vilagra http://google.com/translate_c?u=wshosting.biz/viagra/?page=186 postmaster viagara junkmail http://google.com/translate_c?u=wshosting.biz/viagra/?page=327 viagraa low price usa overnight delivery http://google.com/translate_c?u=wshosting.biz/viagra/?page=232 viaga http://google.com/translate_c?u=wshosting.biz/viagra/?page=52 chennin blanc viagara falls http://google.com/translate_c?u=wshosting.biz/viagra/?page=294 viagara over 4 hours http://google.com/translate_c?u=wshosting.biz/viagra/?page=242 viagara alternatives http://google.com/translate_c?u=wshosting.biz/viagra/?page=429 zenegra vuagra http://google.com/translate_c?u=wshosting.biz/viagra/?page=390 wht is viagras generic drug http://google.com/translate_c?u=wshosting.biz/viagra/?page=258 viagara cheap http://google.com/translate_c?u=wshosting.biz/viagra/?page=188 pseudoephedrine viagara http://google.com/translate_c?u=wshosting.biz/viagra/?page=348 viatra http://google.com/translate_c?u=wshosting.biz/viagra/?page=85 expiration and viagara
    Thanks!
    1001 Post At: 2007-8-28 5:46:21
    abc
    zmvblbrdmo Post At: 2007-11-12 11:49:14
    Hello! Good Site! Thanks you! nwluxthmevnt
    cndqxsdltz Post At: 2008-2-9 20:29:17
    Hello! Good Site! Thanks you! udtyclvdngyblb
    name Post At: 2008-4-16 21:49:29
    yIeUbD
    1 Post At: 2008-5-16 3:22:41
    55
    yTHHApGODIPGIW Post At: 2008-8-18 12:43:40
    qswDpV
    www.guiasite.net.br Post At: 2010-1-25 23:19:16
    Hospedagem Ilimitada LINUX/PHP.
    30 dias grátis - Teste de Qualidade

    Uptime 99,9% - Monitoramento 24 horas

    Recursos Ilimitados:
    . Espaço em disco,
    .Banda.
    .Contas de e-mail.
    .Bancos de dados MySQL.
    .Bancos de dados PostGree.
    . Subdomínios.
    . Apontamentos.

    Acesse nosso Chat Online ou ligue para 11 3020-6679

    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.