**Updated to actually make sense, I hope** Ok folks I am trying to tackle something here but it is over my head. Here is the idea I have a class library that raises events that I want to subscribe to from a winform. When the even is raised I want to use an object in the eventargs to update the form without dealing with a
cast invoke (I have multiple threads going on) in the form code itself. Ultimately I found some code online that works but i'd like to write and extension method for it and I'm failing.
Original <slightly modified=""> working code:
public delegate void StatusLogUpdated(object sender, StatusLoggerUpdatedEventArgs e);
public event StatusLogUpdated StatusChanged;
public void AddLog(LogItem logItem)
{
StatusLogUpdated handler = StatusChanged;
if (handler != null)
{
foreach (StatusLogUpdated singleCast in handler.GetInvocationList())
{
ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke;
if ((syncInvoke != null) && (syncInvoke.InvokeRequired))
{
syncInvoke.Invoke(singleCast, new object[] { this, logEA });
}
else
{
singleCast(this, logEA);
}
}
}
}
Flawed Idea Code
public static void SafeInvoke(this Delegate theDelegate, EventArgs e)
{
Delegate handler = theDelegate;
if (handler != null)
{
foreach (var singleCast in handler.GetInvocationList())
{
ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke;
if ((syncInvoke != null) && (syncInvoke.InvokeRequired))
{
syncInvoke.Invoke(singleCast, new object[] { null, e });
}
else
{
singleCast(null, e);
}
}
}
}
I feel like I'm so close here but I can't seem to figure out what to do instead of the singleCast(null,e); Any thoughts are welcome, I am past my knowledge level. I'm not even fully sure of the ramifications of what I'm trying to do. I'm still pretty new to c# (and programming) so if my terms are off I apologize. Thanks in advance.