38

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?

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
CLaRGe
  • 1,821
  • 1
  • 25
  • 22
  • Note that the docs surrounding Invoke() on Control are actually pretty subtle. I don't believe a generic class is sufficient for Control because of the interaction with IsHandleCreated and IsDisposed (unless you always check those first in your SafeInvokeDelegate). ( http://stackoverflow.com/questions/714666/ ) – Greg D May 05 '09 at 15:30
  • 1
    Thanks for sharing this class. Helped me to solve my problems.. – Farrukh Waheed Apr 01 '16 at 05:32

5 Answers5

95

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
  • 37,778
  • 11
  • 85
  • 87
  • 1
    It may seem obvious, but you also need to add the "System" namespace – Christian Payne Sep 07 '09 at 04:52
  • 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 Oct 08 '10 at 01:52
  • 10
    why not simply `listView1.InvokeEx(lv => lv.Items.Clear());`? – jgauffin Sep 30 '11 at 12:36
  • 1
    This piece of code is fabulistastic. Makes my code nice and clean and easy! As Mark who reminded me of Gump said - Samuel you are a Genius! I like it especially because it works! – Stix Oct 21 '15 at 18:21
  • Can someone please show me what I need to do to incorporate this code in my own form? I have a FormMain (My mainform), I put the block of code in my FormMain.cs class (outside of class FormMain). I got a weird error on the FormMain.designer.cs about not finding the "$this.icon". – rvpals Aug 12 '16 at 17:15
  • @jgauffin Some controls may not implement ISynchronizeInvoke so you cannot use this extension method on them directly. Ex. ToolStripStatusLabel does not implement ISynchronizeInvoke. – Arman Aug 06 '18 at 11:07
  • 1
    @arman: you can change the `T` restriction to `Control` or similar if that fits your use case better. – jgauffin Aug 06 '18 at 11:16
10

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Could you show a code example? :-) Implementing ISynchronizeInvoke requires BeginInvoke, etc, and could get tedious. – CLaRGe Apr 02 '09 at 20:28
  • Looks like ISynchronizeInvoke is implemented only by Control class. This doesn't looke like a way to get rid of Windows Forms dependency. – XOR Apr 02 '09 at 20:33
  • No code example is required. You just replace System.Windows.Forms.Control with System.ComponentModel.ISynchronizeInvoke. – Samuel Apr 02 '09 at 20:36
  • @XOR: ISynchronizeInvoke is in the System assembly, I don't think you will need to reference the Forms assembly. – Samuel Apr 02 '09 at 20:37
  • @XOR: Confirmed, ISynchronizeInvoke only requires a reference to the System library. – Samuel Apr 02 '09 at 20:38
8

Nowadays it's easy to Invoke.
e.g. Say we want to invoke a Label(lblVal) to get value of txtVal

lblVal.Invoke((MethodInvoker)delegate{lblVal.Text = txtVal.Text;});

...it's as easy as that :D

Ctrl S
  • 1,053
  • 12
  • 31
Tj Laubscher
  • 205
  • 4
  • 8
5

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
  • 5,728
  • 7
  • 43
  • 70
  • Hi. I've have been using your code above with great success, but I tested my program on a few computers. It has worked correctly on most of the Win XP computers that I've tested it on, but on one of them I get the following error: "Commom Language Runtime detected an invalid program". Do you know how to solve this? I am using .NET framework V2. – Kritz Aug 28 '11 at 15:50
  • Try upgrading the CLR? I think that Sub lambda is supported only by .Net 4, before there there was only Function lambda. – Eyal Sep 10 '11 at 05:59
  • That doesn't look similar to Samuel's answer to me. You are creating delegates. He uses Lambdas instead of delegates. I will post the VB code that i use in my own answer. – Shawn Kovac Nov 12 '13 at 22:27
  • 1
    The code that I wrote is quite old. At the time, I think that VB.Net didn't have full lambda support. – Eyal Nov 15 '13 at 10:34
4

here's the VB equivalent code to Samuel's answer that i use. notice i actually have 2 extensions functions, but i must admit i don't know why they are there. i copied my C# version years ago (maybe from this site) and it had both extension functions, but for what reason, i don't fully understand. i just copied it and how to use it, and i half understand all that goes on 'under the hood' with these complicated functions.

#Const System_ComponentModel = True
#Const System_Drawing = False

Option Compare Binary
Option Explicit On
Option Strict On

Imports System.Collections
Imports System.Runtime.CompilerServices ' for Extension() attribute
Imports System.Text
#If System_ComponentModel Then
Imports System.ComponentModel
#End If
#If System_Drawing Then
Imports System.Drawing
#End If

Public Module MyExtensions

    ' other #Region blocks are removed. i use many in my Extensions
    ' module/class. the below code is only the 2 relevant extension
    ' for this 'SafeInvoke' functionality. but i use other regions
    ' such as "String extensions" and "Numeric extensions". i use
    ' the above System_ComponentModel and System_Drawing compiler
    ' directives to include or exclude blocks of code that i want
    ' to either include or exclude in a project, which allows me to
    ' easily compare my code in one project with the same file in
    ' other projects to syncronise new changes across projects.
    ' you can scrap pretty much all the code above,
    ' but i'm giving it here so you see it in the full context.

    #Region "ISynchronizeInvoke extensions"

    #If System_ComponentModel Then

        <Extension()>
        Public Function SafeInvoke(Of T As ISynchronizeInvoke, TResult)(isi As T, callFunction As Func(Of T, TResult)) As TResult
            If (isi.InvokeRequired) Then
                Dim result As IAsyncResult = isi.BeginInvoke(callFunction, New Object() {isi})
                Dim endresult As Object = isi.EndInvoke(result)
                Return DirectCast(endresult, TResult)
            Else
                Return callFunction(isi)
            End If
        End Function

        ''' <summary>
        ''' This can be used in VB with:
        ''' txtMyTextBox.SafeInvoke(Sub(d) d.Text = "This is my new Text value.")
        ''' or:
        ''' txtMyTextBox.SafeInvoke(Sub(d) d.Text = myTextStringVariable)
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="isi"></param>
        ''' <param name="callFunction"></param>
        ''' <remarks></remarks>
        <Extension()>
        Public Sub SafeInvoke(Of T As ISynchronizeInvoke)(isi As T, callFunction As Action(Of T))
            If isi.InvokeRequired Then
                isi.BeginInvoke(callFunction, New Object() {isi})
            Else
                callFunction(isi)
            End If
        End Sub

    #End If

    #End Region

    ' other #Region blocks are removed from here too.

End Module

And the C# version is:

#define System_ComponentModel
#undef  System_Drawing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if System_ComponentModel
using System.ComponentModel;
#endif
#if System_Drawing
using System.Drawing;
#endif

namespace MyCompany.Extensions
{
    static partial class MyExtensions
    {

        // other #Region blocks are removed. i use many in my Extensions
        // module/class. the below code is only the 2 relevant extension
        // for this 'SafeInvoke' functionality. but i use other regions
        // such as "String extensions" and "Numeric extensions". i use
        // the above System_ComponentModel and System_Drawing compiler
        // directives to include or exclude blocks of code that i want
        // to either include or exclude in a project, which allows me to
        // easily compare my code in one project with the same file in
        // other projects to syncronise new changes across projects.
        // you can scrap pretty much all the code above,
        // but i'm giving it here so you see it in the full context.

        #region ISynchronizeInvoke extensions
#if System_ComponentModel

        public static TResult SafeInvoke<T, TResult>(this T isi, Func<T, TResult> callFunction) where T : ISynchronizeInvoke
        {
            if (isi.InvokeRequired)
            {
                IAsyncResult result = isi.BeginInvoke(callFunction, new object[] { isi });
                object endResult = isi.EndInvoke(result); return (TResult)endResult;
            }
            else
                return callFunction(isi);
        }

        /// <summary>
        /// This can be used in C# with:
        /// txtMyTextBox.SafeInvoke(d => d.Text = "This is my new Text value.");
        /// or:
        /// txtMyTextBox.SafeInvoke(d => d.Text = myTextStringVariable);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isi"></param>
        /// <param name="callFunction"></param>
        public static void SafeInvoke<T>(this T isi, Action<T> callFunction) where T : ISynchronizeInvoke
        {
            if (isi.InvokeRequired) isi.BeginInvoke(callFunction, new object[] { isi });
            else
                callFunction(isi);
        }

#endif
        #endregion

        // other #Region blocks are removed from here too.

    } // static class MyExtensions

} // namespace

Happy coding!

Shawn Kovac
  • 1,425
  • 15
  • 17