Jump to content


Photo

Handlers hang on vb.net 2005


  • Please log in to reply
7 replies to this topic

#1 Gambit

Gambit
  • Members
  • 12 posts

Posted 05 April 2007 - 07:03 PM

I'm having some wierd issues with handlers working in vb.net 2005.

Not sure if I'm missing something here but I'm basing this off the sample and porting it over to use a form instead of command.

I start off using

[codebox]Public Class Form1
Public objFTP As FTPConnectionMTA
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objGlobal As sfFTPLib.[Global]
'objGlobal = CreateObject("sfFTPLib.Global")
objGlobal = New sfFTPLib.[Global]
objFTP = New sfFTPLib.FTPConnectionMTA
...licnence info here...
' Attach the event handler
AddHandler objFTP.OnConnect, AddressOf _OnConnect
AddHandler objFTP.OnChangeDirectory, AddressOf _OnChangeDirectory
AddHandler objFTP.OnTransferProgress, AddressOf _OnTransferProgress
[/codebox]
for the handlers I do
[codebox] Private Sub _OnChangeDirectory(ByVal szDirectory As String)
TextBox2.Text = TextBox2.Text & "OnChangeDirectory(" & szDirectory & ")"
End Sub

Private Sub _OnTransferProgress(ByVal nBytesTransferredLo As Integer, ByVal nBytesTransferredHi As Integer)
TextBox2.Text = TextBox2.Text & "OnTransferProgress(" & objFTP.LastTransferBytes & ")"
End Sub

Private Sub _OnConnect()
TextBox2.Text = TextBox2.Text & "Connected"
End Sub[/codebox]

For some reason as soon as it tryed to write to TextBox2 or any other UI element it hangs.
I'm I missing something here?

Thanks

#2 mb

mb

    Developer

  • Administrators
  • 11525 posts
  • Gender:
    Male
  • Location:
    Worldwide

Posted 05 April 2007 - 08:26 PM

Hello ...

UI elements always need to be updated in the main (UI) thread. There are two solutions:
1. Use the STA class (FTPConnectionSTA). Then all FTP events come from the main thread. I strongly recommended not to use the STA class.
2. Update the UI elements in the UI thread. I'm not sure how exactly this is done in vb.net but in C++ we send a message to the UI thread in the FTP event handlers. And the message handler in the UI thread then updates the UI controls.

I hope this helps.

Regards,
-Mat

#3 Gambit

Gambit
  • Members
  • 12 posts

Posted 06 April 2007 - 04:48 PM

I've tried everything from BackgroundWorkers to Invoke

I think Invoke is what I need to use but I'm not having any success

multitreading is very new to me and I'm stuck.

I trying to use [codebox]Delegate Sub ChangeTextControlDelegate(ByVal aTextBox As TextBox, ByVal newText As String)[/codebox]

[codebox]Private Sub _OnConnect()

If Me.TextBox2.InvokeRequired Then
Me.TextBox2.Invoke( _
New ChangeTextControlDelegate( _
AddressOf SetDisplayText), New Object() {TextBox2, "connected"})
Else
Me.SetDisplayText(TextBox2, _
"connected")
End If
End Sub

Public Sub SetDisplayText(ByVal aTextBox As TextBox, ByVal newText As String)
aTextBox.AppendText(newText & System.Environment.NewLine)
End Sub[/codebox]
I'm still having the same problem.
From what I read using the invoke and Delegate it's suppost to call the main thread that my ui components are on.

mb or anyone else have any suggestions?

mb could you give me a example on what you do in c++ and maybe I can convert it to vb.net

very <_< right now with this

Thanks for the help

#4 mb

mb

    Developer

  • Administrators
  • 11525 posts
  • Gender:
    Male
  • Location:
    Worldwide

Posted 06 April 2007 - 07:12 PM

Hello ..

The following articles may help you:
http://infosysblogs....rol_access.html
http://channel9.msdn...x?PostID=171594

These links are the result from the following query:
http://www.google.ch...controls thread

The way we do it in C++ will not help you in a managed environment like .net.

Regards,
-Mat

#5 Gambit

Gambit
  • Members
  • 12 posts

Posted 11 April 2007 - 08:36 PM

Hello ..

The following articles may help you:
http://infosysblogs....rol_access.html
http://channel9.msdn...x?PostID=171594

These links are the result from the following query:
http://www.google.ch...controls thread

The way we do it in C++ will not help you in a managed environment like .net.

Regards,
-Mat


I'm dying here...I can't get this to work no matter what I try. From all the reading I have done it seems like the
Invoke method and Delegates should work just fine. But again when I call the invoke method it hangs.

I can get it to work if I use BeginInvoke but that will not let me pass any vars back to the UI control

so no one has run into this problem using .net yet?
Here is a link I found that I used that talks about invoke and stating that you can talk to the UI from another thread:

http://www.vbforums....controls thread

I wish I could find an example using this library and talking to the UI.

Thanks again for the pointers so far.

#6 mb

mb

    Developer

  • Administrators
  • 11525 posts
  • Gender:
    Male
  • Location:
    Worldwide

Posted 12 April 2007 - 10:56 AM

Hello ...

BeginInvoke or Invoke is the right approach. The following article explains the concept nicely. It also comes with an example:
http://www.codeproje...begininvoke.asp

Regards,
-Mat

#7 Gambit

Gambit
  • Members
  • 12 posts

Posted 12 April 2007 - 02:53 PM

Hello ...

BeginInvoke or Invoke is the right approach. The following article explains the concept nicely. It also comes with an example:
http://www.codeproje...begininvoke.asp

Regards,
-Mat


I agree that this is the right approach, but for the life of me I can't ever get invoke to work. BeginInvoke I can get to work without a problem but again this is called after the thread is finished (well it seems that way to me). I'm trying to pass nBytesTransferredHi in the OnTransferProgress but I can't get the ui to update till the download is finished. I try to use Invoke instead but it hangs. Any Idea why Invoke would cause a hang?

#8 mb

mb

    Developer

  • Administrators
  • 11525 posts
  • Gender:
    Male
  • Location:
    Worldwide

Posted 12 April 2007 - 11:43 PM

The problem is that you are calling the functions from the FTP component in your UI thread:
UI Thread:
m_objFTP->DownloadFile()

This is wrong. You have to call the FTP functions from another thread.

Regards,
-Mat




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users