Solved

Calling a method asynchronously:  “Expression does not produce a value.”

Posted on 2006-10-24
6
1,300 Views
Last Modified: 2012-06-21
I’m experimenting with Threading and asynchronous calls.  I had these calls working great in VS2003, but now things have changed in VS2005 and I'm having problems.  I have a test that I’m performing but ran into a snag.  You can see the line with the Messagebox.Show that is giving the error “Expression does not produce a value”.  I’ve stared at this for the longest time and it looks right to me.  Can you spot where I’m going wrong?  My first section of code is from the Windows Form and the 2nd section is another Class that has a very simple subroutine to similate a long call to a database or network slowness.  

Or maybe you think I'm going about this the hard way and that there's a better way to put Subs and Functions into another Thread.  Thank you for looking at this.

I’m using Visual Studio 2005 with .NET 2.0, with VB.NET.  

Imports System.Runtime.Remoting.Messaging
Imports System.Windows.Forms
Imports System.IO
Public Class frmLogger
Private Delegate Sub DoTest()      
Private myDoTest As DoTest = New DoTest(AddressOf Logger.readTHIS)    
Public Sub myTestCallBack(ByVal myAr As IAsyncResult)  
    MessageBox.Show(myDoTest.EndInvoke(myAr)) 'This line shows error on compiling:“Expression does not produce a value.”
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    Dim myCallBack As AsyncCallback = New AsyncCallback(AddressOf Me.myTestCallBack)  
    Dim myAr As IAsyncResult = myDoTest.BeginInvoke(myCallBack, Nothing)
End Sub
End Class

Imports System.IO
Imports System.Threading
Public Class Logger
Public Shared Sub readTHIS()
    frmLogger.Label1.Text = "We are starting"
    Threading.Thread.Sleep(3000)
    frmLogger.Label1.Text = "After 3 second sleep"
    frmLogger.Label1.Refresh()
    Threading.Thread.Sleep(5000)
    frmLogger.Label1.Text = "After 5 second sleep"
    frmLogger.Label1.Refresh()
End Sub
End Class
0
Comment
Question by:psmithphil
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17801826
Not sure if this will help...
  http://www.samspublishing.com/articles/article.asp?p=23020&seqNum=4&rl=1

It explains about Asynchronous Processing with Delegates.

Alternatively, you can download this MSI from microsoft. It holds 101 vb.net examples including Threading, sockets, Asynchronous delegates. Its very handy to see how things are supposed to be done 'properly'.
  http://www.microsoft.com/downloads/details.aspx?familyid=08e3d5f8-033d-420b-a3b1-3074505c03f3&displaylang=en

I'd love to be able to just tell you straight off whats wrong with your code, but I do it a different way, so this is the best I can do im afraid. A guess would be changing the .EndInvoke on the erroring line, to invoke, or another function. Not sure if EndInvoke actually executes anything, I thought it would only stop an invoke?

Hope it helps,
Dez
0
 

Author Comment

by:psmithphil
ID: 17802192
Thank you, Dez, I'll check out what you provided.  I may be going about it wrong.  Could you provide an example of how you do it in VS2005?

I very much appreciate your help!
0
 
LVL 9

Accepted Solution

by:
tony_813 earned 500 total points
ID: 17802253
Hello,
   By looking at your code... your delegate's signature is based on no returned value:   DoTest()
   To my understanding... you will need to change the signature of your delegate say: DoTest() As String
   Then convert the method of your delegate to return a value.....

           Public Shared Function readTHIS() As String
           ----
           Return yourValue
           End Function
           
   That will allow you to use the following.. and return the value in the MessageBox... in the following:
 
      Public Sub myTestCallBack(ByVal myAr As IAsyncResult)  
        MessageBox.Show(myDoTest.EndInvoke(myAr))
      End Sub

tony_813
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 9

Expert Comment

by:tony_813
ID: 17802345
A better option would be to use the last parameter of BeginInvoke to pass the string of Label1 and you wont
have to change your delegate.....

Public Class frmLogger
    Private Delegate Sub DoTest()
    
    Private myDoTest As DoTest = New DoTest(AddressOf Logger.readTHIS)
  
    Public Sub myTestCallBack(ByVal myAr As IAsyncResult)
        Dim msg As String = CType(myAr.AsyncState, String)  
        MessageBox.Show(msg)
    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnTest.Click
        Dim myCallBack As AsyncCallback = _
            New AsyncCallback(AddressOf Me.myTestCallBack)  
        Dim myAr As IAsyncResult = myDoTest.BeginInvoke(myCallBack, _
            frmLogger.Label1)
    End Sub
End Class

tony_813
   
0
 
LVL 9

Expert Comment

by:tony_813
ID: 17802593
Sorry i'm bouncing from here and work....

following line correction:

   from:
         Dim myAr As IAsyncResult = myDoTest.BeginInvoke(myCallBack, _
            frmLogger.Label1)

   To:
          Dim myAr As IAsyncResult = myDoTest.BeginInvoke(myCallBack, _
            frmLogger.Label1.Text)

I'm not sure even if i'm in the right path... i may have jumped the gun... and you may be trying to get some other
value....
LOL
tony_813

0
 

Author Comment

by:psmithphil
ID: 17804374
Tony, I didn't mind changing my Delegate back to a Function, so that's the way I will do it in the future.  I've awarded the points to you on the basis of your first answer and will note what you said about the other things.  It is now working fine!

I'm just starting to use Threading in VS2005 and I have the feeling I'll run into more problems as my Threading gets more complex.  I know I want to use asp.net to hit databases asynchronously, so that will certainly open more Pandora's boxes.  I hope you'll look for my future questions!  :o)

Thank you so much for your outstanding help on this frustrating issue!

And thank you DjDezmond for your comments as well!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

827 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