We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to return a value from a invoked function?

Medium Priority
2,534 Views
Last Modified: 2012-05-06
LS,

The following code I wrote is not working. I expected this, but is there any workaround?

Kind regards,

Matthias Vance
Private Sub t_Test()
        MsgBox(Test2().ToString()) ' It will show "None"
    End Sub
 
    Private Delegate Sub dlg_Test()
    Private Function Test2() As DialogResult
        If Me.InvokeRequired Then
            Me.Invoke(New dlg_Test(AddressOf Test2))
        Else
            Return frmTest.ShowDialog() ' It's "Cancel" here.
        End If
    End Function
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim t As New Thread(AddressOf t_Test)
        t.IsBackground = True
        t.Start()
 
    End Sub

Open in new window

Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
What do you want to do ? When I create a new project, it will open the frmTest here...

Author

Commented:
It's about the DialogResult value it returns.
On line 10: It is "Cancel", as expected
But when the codes goes "back" to line 2, it prints "None" instead.

I attached some sample code which doesn't use a secondary form.
The code will show 0 instead of 4.
Private Sub t_Test()
        MsgBox(Test2.tostring())
    End Sub
 
    Private Delegate Function dlg_Test() As Integer
    Private Function Test2() As Integer
        If Me.InvokeRequired Then
            Me.Invoke(New dlg_Test(AddressOf Test2))
        Else
            Return ReturnValue()
        End If
    End Function
 
    Private Function ReturnValue() As Integer
        Return 4
    End Function
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim t As New Thread(AddressOf t_Test)
        t.IsBackground = True
        t.Start()
    End Sub

Open in new window

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Your delegate needs a parameter :)

        Dim t As New Thread(AddressOf Test2)
        t.IsBackground = True
        t.Start()

    Delegate Sub OutputUpdateDelegate(ByVal data As String)
 
    Public Sub Test2(ByVal data As String)
        If Me.InvokeRequired Then
            Me.Invoke(New OutputUpdateDelegate(AddressOf Test2), New Object() {data})
        Else
            Dim returnResult As DialogResult = Dialog1.ShowDialog()
            OutputUpdateCallback(returnResult)
        End If
        'call directly 
    End Sub
 
    Private Sub OutputUpdateCallback(ByVal data As DialogResult)
        MessageBox.Show(data.ToString())
    End Sub

Open in new window

Author

Commented:
It seems your sample code doesn't use a thread which is started?
I need the result back in my thread.

Regards,

Matthias
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
I've used almost the same code as you provided


Delegate Sub OutputUpdateDelegate(ByVal data As String)
 
    Public Sub Test2(ByVal data As String)
        If Me.InvokeRequired Then
            Me.Invoke(New OutputUpdateDelegate(AddressOf Test2), New Object() {data})
        Else
            Dim returnResult As DialogResult = Dialog1.ShowDialog()
            OutputUpdateCallback(returnResult)
        End If
        'call directly 
    End Sub
 
    Private Sub OutputUpdateCallback(ByVal data As DialogResult)
        MessageBox.Show(data.ToString())
    End Sub
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim t As New Thread(AddressOf t_Test)
        t.IsBackground = True
        t.Start()
    End Sub

Open in new window

Author

Commented:
Yes, but can you provide me your t_Test function?
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
I don't see why you need that function ...
The OutputUpdateCallback-function will return the result back to your thread ....

Author

Commented:
That t_Test function IS the function that is being threaded.

1) Start threaded function t_Test
2) Call external function (Test2) which we need the value from
3) The external function will check if we need to Invoke on the GUI thread, if so, we just Invoke (no data needed here since no action is performed yet)
4) After succesfully invoking, return the DialogResult of frmTest.ShowDialog to the thread. (Note: It will have a value of DialogResult.Cancel here)
5) After succesfully switching back to the calling thread, we assign the value to a variable, it will show DialogResult.None instead of the DialogResult.Cancel we are expecting.
I got it working, you need to replace line 8 of my first reply's sample code with:

Return CInt(Me.Invoke(New dlg_Test(AddressOf Test2)))

Regards,

Matthias

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.