Link to home
Start Free TrialLog in
Avatar of psmithphil
psmithphilFlag for United States of America

asked on

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

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
Avatar of DjDezmond
DjDezmond
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of psmithphil

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of tony_813
tony_813
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
   
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

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!