public static T SafeInvoke <T>( this WebBrowser webBrowser, Func <WebBrowser, T> call) { if (!webBrowser.Visible) { throw new InvalidOperationException("The control is not visible."); } if (!webBrowser.InvokeRequired) { return(call(webBrowser)); } var result = webBrowser.BeginInvoke(call, webBrowser); var endResult = webBrowser.EndInvoke(result); return((T)endResult); }