Reputation: 3084
I need to convert C# code to VB.NET code (based on .NET 3.5 and VS 2008), but I am having problem converting C# delegates to its VB.NET equivalent.
The C# code I want to convert, which works properly, is here:
protected override void OnOwnerInitialized()
{
if (!MobileApplication.Current.Dispatcher.CheckAccess())
{
// MobileApplication.Current is some 3rd party API
// MobileApplication.Current.Dispatcher is type System.Windows.Threading.Dispatcher
MobileApplication.Current.Dispatcher.BeginInvoke
(
(System.Threading.ThreadStart)delegate()
{
OnOwnerInitialized();
}
);
return;
}
DoSomething();
}
I translated to the following VB.NET code but it does not work:
Protected Overrides Sub OnOwnerInitialized()
If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
MobileApplication.Current.Dispatcher.BeginInvoke(Function() New System.Threading.ThreadStart(AddressOf OnOwnerInitialized))
Return
End If
' I also tried the following but I get thread related errors elsewhere in code
'If ((Not MobileApplication.Current.Dispatcher.CheckAccess()) And (Not threadStarted)) Then
' Dim newThread As New Thread(AddressOf OnOwnerInitialized)
' newThread.Start()
' threadStarted = True ' this is a shared / static variable
' Return
'End If
DoSomething()
End Sub
With the C# code, OnOwnerInitialized() gets called twice. On the first call, the function exists with the 'return;' statement; on the second time 'DoSomething() is called. This is the correct behaviour. However, in the VB.NET code, it only runs once, the code does returns on the 'Return' statement and that is it (I believe my translated VB.NET code is not invoking the thread correct. The code below is the C# code).
Thanks.
Upvotes: 2
Views: 1077
Reputation: 3084
I think I got it:
Protected Overrides Sub OnOwnerInitialized()
If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
MobileApplication.Current.Dispatcher.BeginInvoke(DirectCast(Function() DoSomethingWrapper(), System.Threading.ThreadStart))
Return
End If
End If
Private Function DoSomethingWrapper() As Boolean
DoSomething()
Return True ' dummy value to satisfy that this is a function
End If
Basically, I think because I am using .NET 3.5, the line below can only take a function, not a sub:
MobileApplication.Current.Dispatcher.BeginInvoke(DirectCast(Function() RunExtension(), System.Threading.ThreadStart))
Now because OnOwnerInitialized() has to be a sub (as it overrides a sub), and BeginInvoke has to take a function, I just added a wrapper function to wrap around DoSomething(), so OnOwnerInitialized() can call DoSomething() via that wrapper function.
Upvotes: 0
Reputation: 8475
Looks like you can shorten it.
http://msdn.microsoft.com/en-us/library/57s77029(v=vs.100).aspx?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.THREADING.THREADSTART)%3bk(VS.OBJECTBROWSER)%3bk(TargetFrameworkMoniker-".NETFRAMEWORK,VERSION%3dV4.0")&rd=true&cs-save-lang=1&cs-lang=vb#code-snippet-2
Protected Overrides Sub OnOwnerInitialized()
If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
MobileApplication.Current.Dispatcher.BeginInvoke(AddressOf OnOwnerInitialized)
Return
End If
DoSomething()
End Sub
Upvotes: 0