Solved

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

Posted on 2006-10-24
6
1,282 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now