how to know when a work in a thread is complete?

11,708

Solution 1

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.

Solution 2

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.

Solution 3

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!

Share:
11,708
Sein Kraft
Author by

Sein Kraft

Just a simple developer.

Updated on July 01, 2022

Comments

  • Sein Kraft
    Sein Kraft almost 2 years

    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.