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.