Function Delegate
-
Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?
Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form
Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
If oFrm Is Nothing Then Return Nothing
If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
Return oFrm
End FunctionThanks :confused: Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
-
Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?
Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form
Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
If oFrm Is Nothing Then Return Nothing
If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
Return oFrm
End FunctionThanks :confused: Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
What do you mean by "it doesn't work"? Does it compile? throw an exception? Return an unexpected result? ...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?
Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form
Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
If oFrm Is Nothing Then Return Nothing
If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
Return oFrm
End FunctionThanks :confused: Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
Hi Nathan, Following is a demo of Funtion Delegates in VB.Net BEGIN CODE Imports System Public Class MainClass Dim emp As New Employee("First Name", "Last Name") Private Delegate Function NumEmployeesDelegate() As Integer Shared Sub Main(ByVal args As String()) Dim show_num As NumEmployeesDelegate show_num = AddressOf Employee.GetNumEmployees Console.WriteLine(show_num().ToString) End Sub End Class Public Class Employee Private m_FirstName As String Private m_LastName As String Private Shared m_NumEmployees As Integer = 0 Public Shared Function GetNumEmployees() As Integer Return m_NumEmployees End Function Public Sub New(ByVal first_name As String, ByVal last_name As String) m_FirstName = first_name m_LastName = last_name m_NumEmployees += 1 End Sub Public Overrides Function ToString() As String Return m_FirstName & " " & m_LastName End Function Private disposedValue As Boolean = False ' To detect redundant calls End Class END CODE Hope thhis helps :)
Regards, John Adams ComponentOne LLC
-
What do you mean by "it doesn't work"? Does it compile? throw an exception? Return an unexpected result? ...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Sorry for leaving that out...it says I'm still in violation of cross-threading. The invoke should of got me on the forms thread but it is not working.
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
-
Hi Nathan, Following is a demo of Funtion Delegates in VB.Net BEGIN CODE Imports System Public Class MainClass Dim emp As New Employee("First Name", "Last Name") Private Delegate Function NumEmployeesDelegate() As Integer Shared Sub Main(ByVal args As String()) Dim show_num As NumEmployeesDelegate show_num = AddressOf Employee.GetNumEmployees Console.WriteLine(show_num().ToString) End Sub End Class Public Class Employee Private m_FirstName As String Private m_LastName As String Private Shared m_NumEmployees As Integer = 0 Public Shared Function GetNumEmployees() As Integer Return m_NumEmployees End Function Public Sub New(ByVal first_name As String, ByVal last_name As String) m_FirstName = first_name m_LastName = last_name m_NumEmployees += 1 End Sub Public Overrides Function ToString() As String Return m_FirstName & " " & m_LastName End Function Private disposedValue As Boolean = False ' To detect redundant calls End Class END CODE Hope thhis helps :)
Regards, John Adams ComponentOne LLC
Hey John, Thank you. I need an example that deals with a Forms UI thread where I have to test if an invoke is required and invoke to get on that form's thread. I know i can do this with a Sub Delegate but the Function Delegate I can't?
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
-
Hello, Does anyone know how to use a Function Delegate? I have used many Sub Delegates but the function escapes me.... Here's my Code...why does this not work?
Public Delegate Function FormsUIThread(ByVal oFrm As Form) As Form
Public Shared Function GetOnFormsUIThread(ByVal oFrm As Form) As Form
If oFrm Is Nothing Then Return Nothing
If oFrm.InvokeRequired Then Return oFrm.Invoke(New FormsUIThread(AddressOf GetOnFormsUIThread), oFrm)
Return oFrm
End FunctionThanks :confused: Nathan
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
Is the error happening inside this function or after the function call when you use the value returned from it? If it is the latter, than the problem is that the call to Invoke only makes GetOnFormsUIThread be on the main thread. The function that called it originally is still on a different thread and any access to the members of the form returned from the function will continue to throw an exception.
-
Sorry for leaving that out...it says I'm still in violation of cross-threading. The invoke should of got me on the forms thread but it is not working.
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
nlarson11 wrote:
The invoke should of got me on the forms thread
The Invoke does execute in the UI thread, but that is totally pointless as all you are doing is returning a reference to the form. The reference doesn't get any different from being returned from another thread, and when you use the reference, you are still using it from your thread. It's whatever you do with the form that you need to call using Invoke.
Despite everything, the person most likely to be fooling you next is yourself.