2

I have a function that is executed. This has some events that fire some Subs.

The function runs on a different thread then the main thread. Problem I have with this is that the subs connected with the event are also called on a different thread. I would need these to be called on the main thread so I can access the interface.

Is this possible?

UPDATE

code sample, this code is executed on the main thread

 Dim url As String = Ftp


If Not url.StartsWith("ftp://") Then url = "ftp://" & url

Dim uri As New Uri(url)

ftpUpload.Host = uri.Host
ftpUpload.UserName = Benutzername
ftpUpload.Password = Passwort
ftpUpload.Port = 21
Dim localDirectory = Path.GetDirectoryName(Datei) & "\"
Dim localFilenameUpload = Path.GetFileName(Datei)
Dim remoteDirectory = uri.AbsolutePath
Dim remoteFilename = Path.GetFileName(Datei)
ftpUpload.UploadAsync(localDirectory, localFilenameUpload, remoteDirectory, remoteFilename)

the very last line calls a function, this is executed on a different thread and has some events. these events fire following sub on the different thread, I would need them on the main thread.

Private Sub Upload_UploadFileCompleted(ByVal sender As Object, ByVal e As UploadFileCompletedEventLibArgs) Handles ftpUpload.UploadFileCompleted

'dothiings with the interface

End Sub
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97
  • 1
    i will probably need this only this time, so a copy n paste code would be better, but liks are good too! thanks! :) – sharkyenergy Jul 07 '13 at 16:47
  • all i have is what i posted.. the rest is inside a dll i am using. i call the function uploadasync, and when its done it calls the sub upload_uploadfilecompleted. unfortunately its called on a different thread. Thanks yve – sharkyenergy Jul 07 '13 at 17:08
  • actually, i cant wait, because i would have to send the program away today but i totally understand you! go to sleep yve you have done more tehn enough! :) thank you very much! – sharkyenergy Jul 07 '13 at 17:11
  • 1
    What specifically are you trying to access in your interface? if your sub is being called on another thread, you need to use the [`Control.Invoke`](http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2) method. You can also search on SO for `Invoke` – Mark Hall Jul 09 '13 at 04:03

1 Answers1

1

This link gives a good answer on how to create a delegate and then call it back to the main thread.

vb.net threading.thread addressof passing variables

Another good generic example:

thread = New System.Threading.Thread(AddressOf DoStuff)
thread.Start()
Private Delegate Sub DoStuffDelegate()
Private Sub DoStuff()
    If Me.InvokeRequired Then
        Me.Invoke(New DoStuffDelegate(AddressOf DoStuff))
    Else
        Me.Text = "Stuff"
    End If
End Sub

http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/

As you have provided no code, it is impossible for me to customise my answer to suit your needs. Instead I have copied a generic solution from MSDN.

Threading can be used in different ways, in vb.net the GUI runs on one thread, so many processes need to be handled on a separate thread to stop the GUI from locking up.

There are many permutations for achieving this, however this code and the links provided from this page, will give you all the information you need to at least get started.

If you cannot get your code working, please feel free to ask another, more specific question showing your code, and I and/or others, would happily assist you.

The example from MSDN.

Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running 
' on a second thread. 
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts. 
    ' It loops ten times, writing to the console and yielding  
    ' the rest of its time slice each time, and then ends. 
    Public Shared Sub ThreadProc()
        Dim i As Integer 
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next 
    End Sub 

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart  
        ' delegate.  The Visual Basic AddressOf operator creates this 
        ' delegate for you. 
        Dim t As New Thread(AddressOf ThreadProc)

        ' Start ThreadProc.  Note that on a uniprocessor, the new  
        ' thread does not get any processor time until the main thread  
        ' is preempted or yields.  Uncomment the Thread.Sleep that  
        ' follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0) 

        Dim i As Integer 
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub 
End Class

See Thread Class:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx

Community
  • 1
  • 1