2010-12-28 2 views

答えて

2
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 

namespace FewAsyncCalls 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var d = new Action(LongErrorProneMethod); 
      AsyncCallback callback = null; 
      callback = new AsyncCallback(r => 
       { 
        try 
        { 
         d.EndInvoke(r); 
        } 
        catch 
        { 
         Console.WriteLine("Async call failed"); 
         d.BeginInvoke(callback, null); 
        } 
       }); 
      d.BeginInvoke(callback, null); 
      Console.ReadLine(); 
     } 

     private static void LongErrorProneMethod() 
     { 
      Console.WriteLine("Running long error prone method."); 
      Thread.Sleep(1000); 
      if (new Random().Next(100) > 10) 
       throw new InvalidOperationException(); 
      else 
       Console.WriteLine("Async call successful"); 
     } 
    } 
} 
関連する問題