i'm trying to switch from VB. Net to C# (both .Net framework and .Net &) so i started to "translate" some function that i use quite common. But i'm facing some problems (i have zero clue in C based languages).
So, this is the working VB .Net function i used for years, is a simple cross-thread solution when i call control from a different thread, task, etc.:
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Module Module1
Private Delegate Sub InvokeThreadSafeMethodDelegate(ByVal Cnt As Control, ByVal Mtd As Expression(Of Action))
Private Delegate Function InvokeThreadSafeFunctionDelegate(Of T)(ByVal Cnt As Control, ByVal Fnc As Expression(Of Func(Of T))) As T
<Extension()>
Public Sub InvokeThreadSafeMethod(ByVal Cnt As Control, ByVal Mtd As Expression(Of Action))
If (Cnt.InvokeRequired) Then
Dim Dlg = New InvokeThreadSafeMethodDelegate(AddressOf InvokeThreadSafeMethod)
Cnt.Invoke(Dlg, Cnt, Mtd)
Else
Mtd.Compile().DynamicInvoke()
End If
End Sub
<Extension()>
Public Function InvokeThreadSafeFunction(Of T)(ByVal Cnt As Control, ByVal Fnc As Expression(Of Func(Of T))) As T
If (Cnt.InvokeRequired) Then
Dim Dlg = New InvokeThreadSafeFunctionDelegate(Of T)(AddressOf InvokeThreadSafeFunction)
Return DirectCast(Cnt.Invoke(Dlg, Cnt, Fnc), T)
Else
Return DirectCast(Fnc.Compile().DynamicInvoke(), T)
End If
End Function
End Module
And i call them both with that:
Me.Label1.InvokeThreadSafeMethod(Sub() Me.Label1.text = "Test")
Dim X as String = Me.Label1.InvokeThreadSafeFunction(Function() Me.Label1.text)
After translateing it to C# i got this:
using System.Linq.Expressions;
internal static class Cls_Comune
{
private delegate void InvokeThreadSafeMethodDelegate(Control C, Expression<Action> M);
private delegate T InvokeThreadSafeFunctionDelegate<T>(Control C, Expression<Func<T>> F);
public static void InvocaMetodoSicuro(this Control C, Expression<Action> M)
{
if (C.InvokeRequired)
{
var D = new InvokeThreadSafeMethodDelegate(InvocaMetodoSicuro);
C.Invoke(D, C, M);
}
else
M.Compile().DynamicInvoke();
}
public static T InvocaFunzioneSicuro<T>(this Control C, Expression<Func<T>> F)
{
if (C.InvokeRequired)
{
var D = new InvokeThreadSafeFunctionDelegate<T>(InvocaFunzioneSicuro);
return (T)C.Invoke(D, C, F);
}
else
return (T)F.Compile().DynamicInvoke();
}
}
Here, in this line return (T)F.Compile().DynamicInvoke();
i got an error about the conversion of Null variables. In VB .Net i have used a Directcast that seems to not be present in C#.
But the main problem is, how i can call them? Because this, give me 2 different errors:
private async void Naviga(string strLnk)
{
await Task.Run(void () =>
{
Txt_Stato.InvocaMetodoSicuro(() => Txt_Stato.Text = "Test");
WW2.InvocaMetodoSicuro(() => WW2.Source = new System.Uri(strLnk, UriKind.Absolute));
string str = WW2.InvocaFunzioneSicuro(() => WW2.Source.ToString());
}
);
}
In the Task, the first line (Txt_Stato) give me "InvocaMetodoSicuro is not a part of ToolStripLabel". As it can't take the extension from the stati class. The second line (WW2 is a WebView2) give me "An expression tree may not contain an assignment operator." The third line (string str...) give me no errors, but i dont know if works because the debug wont start due to the "Null error" in the static class.
Again, i'm trying with self-learning to switch from VB .Net to C# and i want to convert some useful functions that i had in VB .Net. So, if the C# is a mess, think that i just started to code with it 2 hours ago. So, what is the problem in the code?