I am trying to translate some C# codes to VB, it seemed to work, but want to check if it's alright or some improvement ideas. TIA.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ThreadSafeControls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.CustomInvoke(l => l.Text = "Thread Safe Controls");
}
}
public static class MyInvoke
{
public static void CustomInvoke<T>(this T @control, Action<T> toPerform) where T : ISynchronizeInvoke
{
if (@control.InvokeRequired)
@control.Invoke(toPerform, new object[] { @control });
else
toPerform(@control);
}
}
}
and the VB code that I translated :
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.CustomInvoke(Sub(l) l.Text = "Thread Safe Controls")
End Sub
End Class
Public Module MyInvoke
<System.Runtime.CompilerServices.Extension()>
Public Sub CustomInvoke(Of T As ISynchronizeInvoke)(control As T, toPerform As Action(Of T))
If control.InvokeRequired Then
control.Invoke(toPerform, New Object() {control})
Else
toPerform(control)
End If
End Sub
End Module