Solved

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

Posted on 2006-10-24
6
1,270 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

23 Experts available now in Live!

Get 1:1 Help Now