0

I understand publisher-subscriber pattern but my question is if every object of a class has its own copy of the eventhandler. I have searched SE but most of the questions relate to event subscription signatures like C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'.

I have a class:

            namespace VideoClient
            {
                class VideoPlayer
                {
                    Thread ClientThreadProc;
                    [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
                    public static extern Int32 DeleteObject(IntPtr hGDIObj);
                    RtspClient client;
                    IntPtr hBitmap;
                    public string cameranameTemp;

                    public delegate void Source(System.Windows.Media.Imaging.BitmapSource source, Bitmap Image);
                    public event Source SourceHandler;

                    public VideoPlayer(string URL, string Username, string Password)
                    {
                        client = new RtspClient(URL);
                        client.Credential = new System.Net.NetworkCredential(Username, Password);
                        client.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;


                    }

                    public void Play()
                    {
                        client.OnConnect += (sender, args) =>
                        {
                            sender.OnPlay += (sender1, args1) =>
                            {
                                sender1.Client.FrameChangedEventsEnabled = true;
                                sender1.Client.RtpFrameChanged += Client_RtpFrameChanged;
                                sender1.Client.SetReceiveBufferSize(1024 * 1024);
                            };
                            try
                            {
                                sender.StartListening();
                            }
                            catch (Exception ex)
                            {
                                //SignalLost = true;
                                //IsStreaming = false;
                            }
                        };

                        client.OnDisconnect += (sender, args) =>
                        {
                            sender.Client.Disconnect();
                            sender.Client.Dispose();
                            sender.Disconnect();
                            sender.Dispose();
                        };

                        ClientThreadProc = new Thread(client.Connect);
                        ClientThreadProc.IsBackground = true;
                        ClientThreadProc.Start();
                    }

                    public void Stop()
                    {
                        if (ClientThreadProc != null)
                            ClientThreadProc.Abort();
                        client.StopListening();
                    }

                    System.Windows.Media.Imaging.BitmapSource ConvertImage(System.Drawing.Bitmap bmp)
                    {
                        if (bmp == null)
                        {
                            return null;
                        }
                        hBitmap = bmp.GetHbitmap();
                        System.Windows.Media.Imaging.BitmapSource bs =
                            System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                              hBitmap,
                              IntPtr.Zero,
                              System.Windows.Int32Rect.Empty,
                              System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                        DeleteObject(hBitmap);
                        bmp.Dispose();
                        return bs;
                    }

                    void Client_RtpFrameChanged(object sender, Media.Rtp.RtpFrame frame)
                    {
                        if (frame.Complete && frame.HasMarker && frame.Count > 1)
                        {
                            try
                            {
                                string temp = this.cameranameTemp; bool bTemp = client.Connected;
                                Application.Current.Dispatcher.Invoke 
                                (() =>
                                {
                                    if (frame.PayloadTypeByte == RFC2435Stream.RFC2435Frame.RtpJpegPayloadType && client.Listening)
                                    {
                                        var bitmap = (Bitmap)(new RFC2435Stream.RFC2435Frame(frame));
                                        if (bitmap != null)
                                        {
                                            Bitmap image = new Bitmap(bitmap);
                                            SourceHandler(ConvertImage(bitmap), image);
                                        }

                                    }
                                });

                            }
                            catch (Exception ex)
                            { Console.WriteLine(ex.Message.ToString()); }

                        }

                    }

                }
            }

when we initialize an instance of the VideoPlayer class, each instance has its copy of the fields/methods/delegates etc. of the class but does each instance has its own copy of the eventhandler, in the case; Client_RtpFrameChanged. I would assume so because it is a method which is subscribed to an event. But while running the application, this eventhandler is only handling the events of the 'last' instance and all the instances initialized before does not seem to have their own copy of the eventhandler. If this is indeed the case, then how can I make each instance to have their own copy of the eventhadler?

Many thanks.

Community
  • 1
  • 1
man_luck
  • 1,605
  • 2
  • 20
  • 39
  • TLDR. Please read http://sscce.org then rewrite your question. – Aron Aug 29 '14 at 09:21
  • Aron, in one line the question is "does each instance of any class has its own copy of the eventhandler implemented inside the class" – man_luck Aug 29 '14 at 09:27
  • I can read your title. I can not however understand it. Have you tried to write some test code to see what behaviour you get? – Aron Aug 29 '14 at 09:33

1 Answers1

1

I am not sure I follow your question entirely... but maybe this helps.

You have an instance of Class1 and it raises EventA. The instance of Class1 is really in control of the EventA. EventA may have handlers in any number of interested classes, but there is still only one Class1 and one raising of EventA. The delegate called when the event is raised isn't static so it is its own copy (on whatever class), but remember the parameters defined by the delegate may be shared, or the class that raises the event may also be shared as those all come from the raising Class1.

Jay
  • 5,897
  • 1
  • 25
  • 28
  • Thanks D. It goes like I have an instance of Class1 which has an eventhandler (it does not raise any event). Then I create 2 instances of Class1 which are instance1 and instance2. Both the instances should have their own copies of Class1 members (properties/methods and this eventhandler). It appears that though they have their own copies of rest of the members, the eventhadler is shared between the two instances although the eventhandler is neither static nor it uses any static object. Is this the expected behaviour? – man_luck Aug 29 '14 at 13:04
  • I do not have any static/shared member in either Class1 neither the class is static. – man_luck Aug 29 '14 at 13:06
  • You may have multiple instances of your class (VideoPlayer) handling the RtpFrameChanged on Client, but are they each handling the event for the same Client instance? – Jay Aug 29 '14 at 14:41
  • I am not sure what you are suggesting. If you are asking if both the instances are inside the same class then the answer is yes. But the instances itself are not handling the event. – man_luck Aug 29 '14 at 15:08
  • 1
    The summation is this. There is a single event raised and only one instance of that event. You can have as many handlers as you want, but they all handle the same single event. (To include any information they provide to your handlers) – Jay Aug 29 '14 at 15:42