SORU
12 Mayıs 2011, PERŞEMBE


farklı iplik atılan özel durumu yakalamak

Benim yöntem (Method1) yeni bir iş parçacığı olarak çoğaltılır. Bu iş parçacığı için bir yöntem (Method2) yürütme ve exectution sırasında bir özel durum. Arama yöntemi (Method1) Bu özel durum için bilgi almak istiyorum

Method2 atılan Method1 bu durum yakalayabilirim başka var mı?

CEVAP
12 Mayıs 2011, PERŞEMBE


.NET 4ve yukarıda, yeni bir iş parçacığı oluşturmak yerine Task<T> sınıf kullanabilirsiniz. O zaman istisnalar görev nesne üzerinde .Exceptions özellik. Bunu yapmak için 2 yol vardır:

  1. Ayrı bir yöntem: bazı özel durum işleme//göreviplik

    class Program
    {
        static void Main(string[] args)
        {
            Task<int> task = new Task<int>(Test);
            task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
            task.Start();
            Console.ReadLine();
        }
    
        static int Test()
        {
            throw new Exception();
        }
    
        static void ExceptionHandler(Task<int> task)
        {
            var exception = task.Exception;
            Console.WriteLine(exception);
        }
    }
    
  2. Aynı yöntemi: özel durum işleme//arayanıniplik

    class Program
    {
        static void Main(string[] args)
        {
            Task<int> task = new Task<int>(Test);
            task.Start();
    
            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                Console.WriteLine(ex);    
            }
    
            Console.ReadLine();
        }
    
        static int Test()
        {
            throw new Exception();
        }
    }
    

Hangi özel durumu AggregateException olduğunu unutmayın. Tüm gerçek durumlar ex.InnerExceptions özelliği ile availible.

.NET 3.5aşağıdaki kodu kullanabilirsiniz:

  1. // İstisna işlemçocukiplik

    class Program
    {
        static void Main(string[] args)
        {
            Exception exception = null;
            Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), Handler));
            thread.Start();            
    
            Console.ReadLine();
        }
    
        private static void Handler(Exception exception)
        {        
            Console.WriteLine(exception);
        }
    
        private static void SafeExecute(Action test, Action<Exception> handler)
        {
            try
            {
                test.Invoke();
            }
            catch (Exception ex)
            {
                Handler(ex);
            }
        }
    
        static void Test(int a, int b)
        {
            throw new Exception();
        }
    }
    
  2. Veya // istisna işlemarayanıniplik

    class Program
    {
        static void Main(string[] args)
        {
            Exception exception = null;
            Thread thread = new Thread(() => SafeExecute(() => Test(0, 0), out exception));
    
            thread.Start();            
    
            thread.Join();
    
            Console.WriteLine(exception);    
    
            Console.ReadLine();
        }
    
        private static void SafeExecute(Action test, out Exception exception)
        {
            exception = null;
    
            try
            {
                test.Invoke();
            }
            catch (Exception ex)
            {
                exception = ex;
            }
        }
    
        static void Test(int a, int b)
        {
            throw new Exception();
        }
    }
    

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BachelorsPadTv

    BachelorsPad

    17 Ocak 2012
  • GALERNAYA20

    GALERNAYA20

    19 NİSAN 2011
  • modica89

    modica89

    24 HAZİRAN 2007