Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-10-24
6
Medium Priority
?
1,342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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