title: Trying to get asynchronous functions with arguments and returns to work
body: <p>I have been trying to get a function to work asynchronously with a GUI window, and having very limited success.</p>
  
  <p>The code below is my function, XXX, with a delegate and Callback - to allow stopping the GUI thread, without getting an error. </p>
  
  <p>It works though - if it is a Sub, not a function, and if it doesn't have any arguments.</p>
  
  <p>I don't know how to change it so I can check its return value... I have found a little help in some examples, <a href="http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncstate.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncstate.aspx</a> - but they print the return value in the callback, and I can't see how to get it in the caller.</p>
  
  <p>I can't find any way to use arguments in my function.</p>
  
  <pre><code>Private Function XXX_Callback(ByVal ia As IAsyncResult) 
      Dim d As XXXDelegate = CType(CType(ia, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, XXXDelegate)
      d.EndInvoke(ia)
  
      Dim result As AsyncResult = CType(ia, AsyncResult)
      Dim caller As XXXDelegate = CType(result.AsyncDelegate, XXXDelegate)
      Dim returnValue As Boolean = caller.EndInvoke(ia)
  
      XXX_Finish()   ' needs the "vvv argument, I don't know how to get it
      ' The returnValue is here but I don't know how to send it to the caller
  End Function
  
  'Private Function XXX_Finish(ByVal vvv as Boolean) As Boolean
  ' this probably needs to return something, I don't know what/ how to get it
  Private Function XXX_Finish() As Boolean
      ' something 
      myGui.Finish()            
  End Function
  
  ' Private Delegate Function XXXDelegate(ByVal vvv As Integer) As Boolean
  ' Public Function XXX(ByVal vvv As Integer) As Boolean    ' This is what I would like
  Private Delegate Sub XXXDelegate()
  Public Sub XXX() 
      '
      myGui.Update()
      '
  End Sub
  
  Public Sub Caller()
      '
      myGui = New SomeGui()
      myGui.Begin()
      Dim t As New XXXDelegate(AddressOf XXX)
      t.BeginInvoke(AddressOf XXX_Callback, Nothing)
      ' more code, another call
  End Sub
  
  Private myGui As SomeGui
  </code></pre>
  
  <p>Please, could someone help me get this into a better shape, or get some examples that will help ? With the ones I have found in the past two days, I have reached a dead end... </p>
  
  <p>Thank you.</p>