Here's my extension method for invoke on a control:
public static void Invoke<T>(this T c, Action<System.Windows.Forms.Control> DoWhat)
where T:System.Windows.Forms.Control
{
if (c.InvokeRequired)
c.Invoke(o=> DoWhat(c) );
else
DoWhat(c);
}
ds is a strongly typed dataset. This works:
Action<DataGridView> a = row => row.DataSource = ds.bLog;
this.dataGridView1.Invoke(a);
this doesn't compile:
this.dataGridView1.Invoke<DataGridView>(o => o.DataSource = ds.bLog);
and says System.Windows.Forms.Control does not contain a definition for 'DataSource'...
do I really have to break this into 2 lines? For clarity/safety should I call the generic extension method InvokeSafe?
Edit:Extension method revised (works, but I'd like to remove the named delegate requirement):
private delegate void myDel();
public static void InvokeSafe<T>(this T c, Action<T> DoWhat)
where T : System.Windows.Forms.Control
{
myDel d = delegate() { DoWhat(c); };
if (c.InvokeRequired)
c.Invoke(d);
else
DoWhat(c);
}
I can't seem to figure out how to make factor out myDel into an anonymous delegate in the block?