Need some help, when im uploading the form stops responding and the progress bar is not loading. someone can help me in this problem. Thank you so much for advance :) -> Heres my whole code

Imports System


Public Class Form1
    Public filenameOnly As String
    Public FN As String
    Public FN4 As String



    Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Using ofd As New OpenFileDialog()
            If ofd.ShowDialog() = DialogResult.OK Then



                FN = ofd.FileName
                TextBox3.Text = FN
                Dim filePath As String = FN
                Dim slashPosition As ULong = filePath.LastIndexOf("\")
                filenameOnly = filePath.Substring(slashPosition + 1)



            End If
        End Using
    End Sub


    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.DoEvents()
        Timer1.Start()
        Button1.Enabled = False
        Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://localhost/" + filenameOnly), System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile


        Try
            Dim file() As Byte = System.IO.File.ReadAllBytes(TextBox3.Text)
            Dim strz As System.IO.Stream = request.GetRequestStream()
            Dim fsl = file.Length


            ProgressBar1.Value = 0

            If fsl <> 0 Then

                For offset As ULong = 1 To file.Length Step 163840000000



                    ProgressBar1.Value = CType(offset * 100 / fsl, ULong)


                    Dim chunkSize As ULong = file.Length - offset - 1


                    If chunkSize > 16384000000000000 Then chunkSize = 16384000000000000
                    strz.Write(file, offset, chunkSize)

                Next

                strz.Close()
                strz.Dispose()
                Timer1.Stop()

                ProgressBar1.Value = ProgressBar1.Maximum
                If ProgressBar1.Value = 100 Then
                    Button1.Enabled = True
                    MessageBox.Show("Upload Successful!")

                    ProgressBar1.Value = 0
                    Button2.Enabled = True
                    TextBox3.Text = ""
                    Application.Restart()



                End If
            Else
                MessageBox.Show("You cannot Upload a null file")
                Button1.Enabled = True
            End If
        Catch
            ProgressBar1.Value = 0
            Timer1.Stop()

            MessageBox.Show("Invalid Username or Password!")
            If TextBox3.Text = "" Then

                MessageBox.Show("Please browse for any file!")
            End If




        End Try




    End Sub




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        System.Windows.Forms.Application.DoEvents()

        Button1.Enabled = False
        TextBox3.Enabled = False
        Button3.Enabled = False
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo("C:\Users\user\Desktop\Files FTP\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo
        Dim sum As Integer = 0

        'list the names of all files in the specified directory
        For Each dra In diar1
            sum += 1
            ListBox1.Items.Add(dra)
        Next

        ListBox1.Items.Remove(sum)


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Increment(1)



    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Button3.Enabled = True
        FN4 = ListBox1.Text

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Kill("C:\Users\user\Desktop\Files FTP\" + FN4)

        Application.Restart()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Kill("C:\Users\user\Desktop\Files FTP\*.*")
        Application.Restart()
    End Sub

    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click

    End Sub

End Class

Recommended Answers

All 4 Replies

You need to go with a separate thread (or background worker) for your upload. This will keep your main thread free to update your progress bar and make your form responsive.

oh, thankyou. but i dont have any idea about that background worker lol anyway thanks gonna watch some tutorial :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.