views:

62

answers:

3

I need to create multiple threads when a button is clicked and i've done that with this:

Dim myThread As New Threading.Thread(AddressOf getFile)
myThread.IsBackground = True
myThread.Start()

but i need to update a picture box with the downloaded file, buy if i set an event in the function getFile and raise it to notify that the files was downloaded and then update the picturebox.

+6  A: 

Use an AsyncResult, and either check it periodically for completion, or provide a delegate to be called when the thread has completed its work.

A complete example in VB can be found here.

Robert Harvey
+1  A: 

You need to make use of MethodInvoker deligate.

Public Sub GetFile()
    If Me.InvokeRequired Then
        Me.Invoke(New MethodInvoker(GetFile)) 
    End If
End Sub

Now you can handle any event in your specified class.

Ramezanpour
+2  A: 

You can achive that using the Asyncallback, ...

Dim sinctotal As New Del_sinc(AddressOf sincronizar)

Dim ar As IAsyncResult = sinctotal.BeginInvoke(_funcion, type, New AsyncCallback(AddressOf SincEnd), cookieobj)

The cookieobj is this

Class Cookie

Public id As String
Public AsyncDelegate As [Delegate]
Sub New(ByVal id As String, ByVal asyncDelegate As [Delegate])

    Me.id = id
    Me.AsyncDelegate = asyncDelegate

End Sub

End Class

When the delegate finish it will call the funcion Sincend (in this example), then you could use a event to update your picture.

Hope this helps!

carlos