Using this article advice, I have been able to successfully use PostSharp's [BackgroundMethod]
and SafeInvoke
extension from the CodeProject article. Since it has a couple of typos, here is a re-write:
public static TResult SafeInvoke<T,TResult>(this T isi, Func<T,TResult> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired) {
IAsyncResult result = isi.BeginInvoke(call, new object[] { isi });
object endResult = isi.EndInvoke(result); return (TResult)endResult;
}
else
return call(isi);
}
public static void SafeInvoke<T>(this T isi, Action<T> call) where T : ISynchronizeInvoke
{
if (isi.InvokeRequired) isi.BeginInvoke(call, new object[] { isi });
else
call(isi);
}
I hope it saves someone some time.