Melhor maneira de invocar algum código de linha cruzada?
Sei que essa pergunta já foi feita antes, mas estou procurando uma maneira de:
streamline a criação de código seguro de thread cruzad reutilize este código em qualquer situação (sem referências aos Formulários do WindowsAqui está o que tenho até agora, mas quero remover as referências do Windows Forms. Alguma ideia
public delegate void SafeInvokeDelegate(System.Action action);
public class SafeInvoke
{
private readonly System.Windows.Forms.Control _threadControl;
public SafeInvoke()
{
_threadControl = new System.Windows.Forms.Control();
}
public void Invoke(System.Action action)
{
if (_threadControl.InvokeRequired)
_threadControl.Invoke(new SafeInvokeDelegate(Invoke), new object[] {action});
else if (action != null) action();
}
}
A classe acima pode ser usada desta maneira:
SafeInvoke _safeInvoker = new SafeInvoke();
void SafeClearItems()
{
_safeInvoker.Invoke(delegate
{
listView1.Items.Clear();
});
}
Como remover o System.Windows.Forms.Control na classe SafeInvoke, mas manter a mesma funcionalidade?