Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.2k views
in .NET FTP by (120 points)
Hi there,

First I want to say thank you for such a powerful and stable FTP library - I've got an application that really needed one, I started to write my own and then found yours - thank you so much!

Anyway, I wanted to contribute a code sample for others who would like to learn to use this software - I hope this is the right place to post this.

For my application, I wanted to print out the FTP socket activity into a Windows Forms TextBox so the user would have a visual confirmation of status.

So, I created a class called "StatusBoxAppender" which implements the Appender interface.

Imports EnterpriseDT.Util.Debug

Public Class StatusBoxAppender
    Implements Appender

    Private m_Textbox As TextBox = Nothing
    Private Delegate Sub UpdateTextbox(ByVal newStr As String)

    Public Sub New(ByRef theTextBox As TextBox)
        If theTextBox IsNot Nothing Then
            m_Textbox = theTextBox
        End If
    End Sub

    Public Sub Close() Implements EnterpriseDT.Util.Debug.Appender.Close

    End Sub

    Public Sub Log(ByVal msg As String) Implements EnterpriseDT.Util.Debug.Appender.Log
        If m_Textbox IsNot Nothing Then
            If m_Textbox.InvokeRequired Then
                Dim d As New UpdateTextbox(AddressOf Log)
                m_Textbox.BeginInvoke(d, msg)
            Else
                m_Textbox.AppendText(msg & Environment.NewLine)
            End If
        End If
    End Sub

    Public Sub Log(ByVal t As System.Exception) Implements EnterpriseDT.Util.Debug.Appender.Log
        If m_Textbox IsNot Nothing Then
            If m_Textbox.InvokeRequired Then
                Dim d As New UpdateTextbox(AddressOf Log)
                m_Textbox.BeginInvoke(d, t.Message)
            Else
                m_Textbox.AppendText(t.Message & Environment.NewLine)
            End If
        End If
    End Sub
End Class


As you can see, the constructor accepts a TextBox control reference, which it will then append to.

Second, there is a delegate that is used to marshal the text updates to the Control back to the UI thread in case the Logger is being called from a separate thread. In each of the Log methods, the control is checked to see if it must be Invoked in order to be thread-safe, and if so, the delegate is invoked, which calls the Log method again, hopefully on the UI thread.

When the Log method is called on the UI thread, the text is appended to the TextBox for the user to see.


The Close method is empty because nothing needs to be done to clean up the TextBox output.


Then, to use this appender, I did the following:

        Logger.CurrentLevel = Level.DEBUG
        Dim myappender As Appender = New StatusBoxAppender(txtStatusInfo)
        Logger.AddAppender(myappender)


That simple.

Hope this helps someone.

Patrick

1 Answer

0 votes
by (51.6k points)
Thank you very much for contributing this example. We really appreciate your generosity.

- Hans (EDT)

Categories

...