tags:

views:

3130

answers:

4

I know that this question has been asked before, but I'm looking for a way to:

  1. streamline the creation of safe cross-threaded code.
  2. reuse this code in any situation (no Windows Forms references).

Here's what I have so far, but I want to remove the Windows Forms references. Any ideas?

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();
    }
}

The above class might be used this way:

SafeInvoke _safeInvoker = new SafeInvoke();
void SafeClearItems()
{
    _safeInvoker.Invoke(delegate
        {
            listView1.Items.Clear();
        });
}

How would I remove the System.Windows.Forms.Control in the SafeInvoke class but keep the same functionality?

+3  A: 

Use ISynchronizeInvoke instead of Control. That's the interface that Control implements with Invoke/BeginInvoke/EndInvoke/InvokeRequired.

An alternative is to use SynchronizationContext.Current - which is what BackgroundWorker uses, I believe.

Jon Skeet
Damn, when did you break the 50k mark? There should have been a party.
Samuel
Could you show a code example? :-) Implementing ISynchronizeInvoke requires BeginInvoke, etc, and could get tedious.
CLaRGe
Looks like ISynchronizeInvoke is implemented only by Control class. This doesn't looke like a way to get rid of Windows Forms dependency.
XOR
No code example is required. You just replace System.Windows.Forms.Control with System.ComponentModel.ISynchronizeInvoke.
Samuel
@XOR: ISynchronizeInvoke is in the System assembly, I don't think you will need to reference the Forms assembly.
Samuel
@XOR: Confirmed, ISynchronizeInvoke only requires a reference to the System library.
Samuel
+19  A: 

You also could use an extension method and lambdas to make your code much cleaner.

using System.ComponentModel;
public static class ISynchronizeInvokeExtensions
{
  public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
  {
    if (@this.InvokeRequired)
    {
      @this.Invoke(action, new object[] { @this });
    }
    else
    {
      action(@this);
    }
  }
}

So now you can use InvokeEx on any ISynchronizeInvoke and be able to access the properties and fields of implementing class.

this.InvokeEx(f => f.listView1.Items.Clear());
Samuel
It may seem obvious, but you also need to add the "System" namespace
Christian Payne
This is an awesome idea. I did it in VB.net and also have 4 overloads for Subroutines/Functions/WithParams/WithoutParams. VB.net is able to infer the generic types.
Eyal
A: 

Recommended reading: Synchronizing calls to the UI in a multi-threaded application

Igor Brejc
+1  A: 

Here it is in VB.net, very similar to Samuel's answer. I have four overloads depending on whether you want a subroutine or function and whether or not there's a parameter. It would be easy to add more overloads for more parameters. VB.Net is able to infer the types.

Module ISynchronizeInvokeExtensions
    Public Delegate Function GenericLambdaFunctionWithParam(Of InputType, OutputType)(ByVal input As InputType) As OutputType
    Private Delegate Function InvokeLambdaFunctionCallback(Of InputType, OutputType)(ByVal f As GenericLambdaFunctionWithParam(Of InputType, OutputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType
    Public Function InvokeEx(Of InputType, OutputType)(ByVal f As GenericLambdaFunctionWithParam(Of InputType, OutputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType
        If c.InvokeRequired Then
            Dim d As New InvokeLambdaFunctionCallback(Of InputType, OutputType)(AddressOf InvokeEx)
            Return DirectCast(c.Invoke(d, New Object() {f, input, c}), OutputType)
        Else
            Return f(input)
        End If
    End Function

    Public Delegate Sub GenericLambdaSubWithParam(Of InputType)(ByVal input As InputType)
    Public Sub InvokeEx(Of InputType)(ByVal s As GenericLambdaSubWithParam(Of InputType), ByVal input As InputType, ByVal c As System.ComponentModel.ISynchronizeInvoke)
        InvokeEx(Of InputType, Object)(Function(i As InputType) As Object
                                           s(i)
                                           Return Nothing
                                       End Function, input, c)
    End Sub

    Public Delegate Sub GenericLambdaSub()
    Public Sub InvokeEx(ByVal s As GenericLambdaSub, ByVal c As System.ComponentModel.ISynchronizeInvoke)
        InvokeEx(Of Object, Object)(Function(i As Object) As Object
                                        s()
                                        Return Nothing
                                    End Function, Nothing, c)
    End Sub

    Public Delegate Function GenericLambdaFunction(Of OutputType)() As OutputType
    Public Function InvokeEx(Of OutputType)(ByVal f As GenericLambdaFunction(Of OutputType), ByVal c As System.ComponentModel.ISynchronizeInvoke) As OutputType
        Return InvokeEx(Of Object, OutputType)(Function(i As Object) f(), Nothing, c)
    End Function
End Module

Usage (run this in a backgroundworker):

    InvokeEx(Sub(x As String) Me.Text = x, "foo", Me) 'set form title to foo
    InvokeEx(AddressOf MsgBox, Me.Text, Me)
    InvokeEx(Sub() Me.Text &= "!", "foo", Me) 'append "!" to form title
    InvokeEx(AddressOf MsgBox, Me.Text, Me)
    Dim s As String = InvokeEx(Function() Me.Text, Me) & "bar" 'get form title to backgorundworker thread
    InvokeEx(AddressOf MsgBox, s, Me) 'display the string from backgroundworker thread
Eyal