SORU
21 Mart 2011, PAZARTESİ


Bir özel durum zaman uyumsuz bir yöntem tarafından atılan yakalamak

Zaman uyumsuz kullanarak Microsoft için ctp .NET, özel durum bir çağırma yöntemi zaman uyumsuz bir yöntem tarafından atılan yakalamak mümkün mü?

public async void Foo()
{
    var x = await DoSomethingAsync();
    /* Handle the result, but sometimes an exception might be thrown
       For example, DoSomethingAsync get's data from the network
       and the data is invalid... a ProtocolException might be thrown */
}

public void DoFoo()
{
    try
    {
        Foo();
    }
    catch (ProtocolException ex)
    {
          /* The exception will never be caught
             Instead when in debug mode, VS2010 will warn and continue
             when deployed the app will simply crash. */
    }
}

Temelde uyumsuz kodu dışında Yukarı çağrı kodum baloncuk istiyorum bu mümkün olsa bile.

CEVAP
21 Mart 2011, PAZARTESİ


(Mind f*** böyle buluyorum!) okumak için biraz garip geldi ama evet, istisna arayıp kodu baloncuk olacak - ama sadece bunu yapmak olacakawait veya Wait() 5 ** yapılan çağrı

public async void DoFoo()
{
    try
    {
        await Foo();
    }
    catch (ProtocolException ex)
    {
          /* The exception will be caught because you've awaited the call. */
    }
}

//or//

public void DoFoo()
{
    try
    {
        Foo().Wait();
    }
    catch (ProtocolException ex)
    {
          /* The exception will be caught because you've awaited the call. */
    }
} 

Bekle kullanmayı unutmayın() Uygulama, Eğer engellemek için neden olabilir .Net yöntemi eşzamanlı olarak yürütmek için karar verir.

Bu açıklama http://www.interact-sw.co.uk/iangblog/2010/11/01/csharp5-async-exceptions oldukça iyidir - derleyici bu sihirli elde etmek için gereken adımlar açıklanır.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BASS212M

    BASS212M

    15 Temmuz 2009
  • taliajoy18

    taliajoy18

    12 Temmuz 2011
  • Subscribe!!

    Subscribe!!

    3 EKİM 2009