• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2041
  • Last Modified:

How to return a value from a invoked function?

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

0
MatthiasVance
Asked:
MatthiasVance
  • 5
  • 4
1 Solution
 
DhaestCommented:
What do you want to do ? When I create a new project, it will open the frmTest here...
0
 
MatthiasVanceAuthor 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

0
 
DhaestCommented:
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

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Regards,

Matthias
0
 
DhaestCommented:
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

0
 
MatthiasVanceAuthor Commented:
Yes, but can you provide me your t_Test function?
0
 
DhaestCommented:
I don't see why you need that function ...
The OutputUpdateCallback-function will return the result back to your thread ....
0
 
MatthiasVanceAuthor 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.
0
 
MatthiasVanceAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now