?
Solved

How to return a value from a invoked function?

Posted on 2009-02-12
9
Medium Priority
?
1,872 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

0
Comment
Question by:MatthiasVance
  • 5
  • 4
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23621441
What do you want to do ? When I create a new project, it will open the frmTest here...
0
 
LVL 8

Author Comment

by:MatthiasVance
ID: 23621472
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 23621973
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

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

Regards,

Matthias
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23622131
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
 
LVL 8

Author Comment

by:MatthiasVance
ID: 23622163
Yes, but can you provide me your t_Test function?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23622205
I don't see why you need that function ...
The OutputUpdateCallback-function will return the result back to your thread ....
0
 
LVL 8

Author Comment

by:MatthiasVance
ID: 23622236
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
 
LVL 8

Accepted Solution

by:
MatthiasVance earned 0 total points
ID: 23623038
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question