Exemplo n.º 1
0
 public void OnSerializingMethod(StreamingContext context)
 {
     foreach (var d  in MyEvent.GetInvocationList())
     {
         MyEvent -= (PropertyChangedEventHandler)d;
     }
 }
Exemplo n.º 2
0
 public void DisplayEventInvocationList()
 {
     if (MyEvent != null)
     {
         foreach (Delegate d in MyEvent.GetInvocationList())
         {
             Console.WriteLine("Object: {0}, Method: {1}", (d.Target ?? "null").ToString(), d.Method);
         }
     }
 }
 /// <summary>
 /// When disposing unsubscibe from all events
 /// </summary>
 public void Dispose()
 {
     if (MyEvent != null)
     {
         foreach (Delegate del in MyEvent.GetInvocationList())
         {
             MyEvent -= (del as EventHandler);
         }
     }
     // do the same for any other events in the class
     //  ....
 }
Exemplo n.º 4
0
            public void DoSomething()
            {
                //if (MyEvent != null)
                //{
                //    //获取订阅者列表
                //    Delegate[] delArray = MyEvent.GetInvocationList();
                //    foreach (Delegate del in delArray)
                //    {
                //        EventHandler method = (EventHandler)del; // 强制转换为具体的委托类型
                //        try
                //        {
                //            method(this, EventArgs.Empty);
                //        }
                //        catch (Exception e)
                //        {
                //            Console.WriteLine("Exception: {0}", e.Message);
                //        }
                //    }
                //}

                //以上代码可以使用下面的方法代替
                if (MyEvent != null)
                {
                    Delegate[] delArray = MyEvent.GetInvocationList();
                    foreach (Delegate del in delArray)
                    {
                        try
                        {
                            // 使用DynamicInvoke方法触发事件
                            del.DynamicInvoke(this, EventArgs.Empty);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception: {0}", e.Message);
                        }
                    }
                }

                //下面代码一旦有异常发生,会影响其他订阅者的代码不被执行
                // 做某些其他的事情
                //if (MyEvent != null)
                //{
                //    try
                //    {
                //        MyEvent(this, EventArgs.Empty);
                //    }
                //    catch (Exception e)
                //    {
                //        Console.WriteLine("Exception: {0}", e.Message);
                //    }
                //}
            }
Exemplo n.º 5
0
 public void DoSomething()
 {
     Console.WriteLine("Do something invoked");
     if (MyEvent != null)
     {
         Delegate[] delArray = MyEvent.GetInvocationList();
         foreach (var del in delArray)
         {
             EventHandler method = (EventHandler)del;
             method.BeginInvoke(null, EventArgs.Empty, null, null);
         }
     }
 }
Exemplo n.º 6
0
 public void DoSomething()
 {
     if (MyEvent != null)
     {
         Delegate[] delArray = MyEvent.GetInvocationList();
         foreach (Delegate del in delArray)
         {
             try
             {
                 del.DynamicInvoke(this, EventArgs.Empty);
             }
             catch (Exception e)
             {
                 Console.WriteLine("Exception:{0}", e.Message);
             }
         }
     }
 }
Exemplo n.º 7
0
 public void DoSomething()
 {
     Console.WriteLine("DoSomething Invoke !");
     if (MyEvent != null)
     {
         Delegate[] delArray = MyEvent.GetInvocationList();
         foreach (Delegate del in delArray)
         {
             try
             {
                 EventHandler method = (EventHandler)del;
                 method.BeginInvoke(null, EventArgs.Empty, null, null);
                 //对于多个订阅者注册的情况,必须使用GetInvocationList()获得所有委托对象,
                 ///然后遍历它们,分别在其上调用BeginInvoke()方法
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
             }
         }
     }
 }
Exemplo n.º 8
0
 public IEnumerable <EventHandler> GetMyEventHandlers()
 {
     return(from d in MyEvent.GetInvocationList()
            select(EventHandler) d);
 }