1

I just made a ftp chat in vb.net and it update message from a file from a ftp server so i add a timer with interval 1000 with this code

 Try
            Dim client As New Net.WebClient
            client.Credentials = New Net.NetworkCredential("fnet_1355****", "******")
            RichTextBox1.Text = client.DownloadString("ftp://185.**.***.**/htdocs/chat­.txt")
        Catch ex As Exception
        End Try

so .. the file is downloaded and it update the text successful but there is a problem .. every time he download the form have a bit lag ... and i dont like that :D what i can do ?

3 Answers3

3
RichTextBox1.Text = client.DownloadString("ftp://185.**.***.**/htdocs/chat­.txt")

Instead of this try async method.

client.DownloadStringAsync(new Uri("ftp://185.**.***.**/htdocs/chat­.txt"))

and then handle download string completed event.

Sample Code

client.DownloadStringAsync(new Uri("ftp://185.**.***.**/htdocs/chat­.txt"));
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
     RichTextBox1.Text =e.Result;
}

You can also add progress indicator by handling progress change event.

Arpit
  • 12,767
  • 3
  • 27
  • 40
0

The best way you can do it would be to use ThreadPool provided by the Framework to I/O bound operation on different thread.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DownloadFromFtp))
End Sub

Private Sub DownloadFromFtp()
    Try
        Dim client As New Net.WebClient
        client.Credentials = New Net.NetworkCredential("fnet_1355****", "******")
        Dim response As String = client.DownloadString("ftp://185.**.***.**/htdocs/chat­.txt")

        Me.Invoke(New MethodInvoker(Function() RichTextBox1.Text = response))
    Catch ex As Exception
    End Try
End Sub
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
0

This program was the exact one I design before i learned PHP.

Here try this:

Dim thrd As Threading.Thread
Dim tmr As New Timer
Dim tempstring As String
Private Sub thread_start()
    thrd = New Threading.Thread(Sub() check_for_changes())
    tmr.Interval = 50
    AddHandler tmr.Tick, AddressOf Tick
    tmr.Enabled = True
    tmr.Start()
    thrd.Start()
End Sub
Private Sub Tick(sender As Object, e As EventArgs)
    If Not thrd.IsAlive Then
        tmr.Stop() : tmr.Enabled = False
        RichTextBox1.Text = tempstring
    End If
End Sub
Private Sub check_for_changes()
    Try
        Dim client As New Net.WebClient
        client.Credentials = New Net.NetworkCredential("fnet_1355****", "******")
        tempstring = client.DownloadString("ftp://185.**.***.**/htdocs/chat­.txt")
    Catch ex As Exception
    End Try
End Sub

Hope It Helps.

aliqandil
  • 1,673
  • 18
  • 28