VB.NET Pemula : Membuat Internet Download Manager + BackgroundWorker + ProgressBar

Belajar Bahasa pemrograman VB.NET pemula : cara membuat Internet Download Manager lengkap dengan informasi download dan Background worker serta progressbar di visual studio 2015

Belajar bahasa pemrograman vb.net - scqq.blogspot.com kali ini akan membahas bagaimana cara mudah membuat aplikasi sederhana Download Manager menggunakan bahasa basic. Aplikasi Internet Download Manager ini lengkap dengan informasi download request dan response, juga dilengkapi dengan Background worker serta Progressbar agar bisa mengupdate informasi Speed Download. penasaran bagaimana cara membuatnya ? jangan kemana-mana ya..

Internet Download Manager Vb.NET

Langsung saja, silahkan buka aplikasi visual studio kamu, boleh menggunakan visual studio 2010. dalam tutorial ini penulis membuat aplikasi IDM menggunakan vs 2015. Buatlah project baru dengan nama "InternetDownloadManager" . Pada form1.vb boleh kamu rubah namanya sesuai kebutuhan, serta desainlah tampilan form1 dengan tampilan seperti gambar dibawah ini :


Tambahkan juga component BackgroundWorker dan ProgressBar pada form1. jika sudah mari kita mualai berpusing rianya.

Code VB.NET Form1_Load

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label6.Text = ""
    End Sub
Berfungsi untuk mengosongkan tulisan label6.text saat aplikasi load.

Code VB.NET tombol Download (Button1)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
            'we will create the save file dialog here
            Me.SaveFileDialog1.FileName = Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
            If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.wheretosave = Me.SaveFileDialog1.FileName
                Me.SaveFileDialog1.FileName = ""
                Label3.Text = "Save to : " & Me.wheretosave
                Me.TextBox1.Enabled = False
                Me.Button1.Enabled = False
                Me.Button2.Enabled = True
                Me.BackgroundWorker1.RunWorkerAsync() ' start our download
            End If
        Else
            MessageBox.Show("Warning : Please insert valid URL for download", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub
Selanjutnya letakkan variable berikut dibawah class form1
    Dim wheretosave As String ''Where the program save the file
    Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
    Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

Code BackgroundWorker

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        ' creating the request and getting the response
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest

        Try 'check if the file is exist

            theRequest = WebRequest.Create(Me.TextBox1.Text)
            theResponse = theRequest.GetResponse

        Catch ex As Exception

            MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf &
                            "1) File doesn't exist" & ControlChars.CrLf &
                            "2) Remote server error", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            'we will create the delegate here
            ' just wait fo a moment

            Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
            Me.Invoke(cancelDelegate, True)
            Exit Sub
        End Try

        Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
        ' we will create the functions for update the informations
        ' just wait for a moment

        Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
        Me.Invoke(safedelegate, lenght, 0, 0, 0)

        Dim writestream As New IO.FileStream(Me.wheretosave, IO.FileMode.Create)

        'Replacement for Stream.Position (webResponse stream doesn't support seek)
        Dim nRead As Integer

        'To calculate the download speed
        Dim speedTimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0

        Do
            If BackgroundWorker1.CancellationPending Then 'If user abort download
                Exit Do
            End If
            speedTimer.Start()
            Dim readBytes(4095) As Byte
            Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
            nRead += bytesread

            Dim percent As Short = (nRead * 100) / lenght
            Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)

            ' sorry for it, just replace the variable speed to double
            ' lets try it again

            If bytesread = 0 Then Exit Do

            writestream.Write(readBytes, 0, bytesread)
            speedTimer.Stop()

            readings += 1
            If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles

                currentspeed = 20480 / (speedTimer.ElapsedMilliseconds / 1000)
                speedTimer.Reset()
                readings = 0
            End If

        Loop

        'Close the streams
        theResponse.GetResponseStream.Close()
        writestream.Close()

        If Me.BackgroundWorker1.CancellationPending Then

            IO.File.Delete(Me.wheretosave)
            Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
            Me.Invoke(canceldelegate, True)
            Exit Sub
        End If

        Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
        Me.Invoke(complatedelegate, False)
    End Sub

Functions ChangeText

    Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
        Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
        Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "

        If speed = -1 Then

            Me.Label5.Text = "Speed : Calculating ..."
        Else
            Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent

    End Sub

Functions Download Complate

    Public Sub DownloadComplate(ByVal cancelled As Boolean)

        Me.TextBox1.Enabled = True
        Me.Button1.Enabled = True
        Me.Button2.Enabled = False

        If cancelled Then

            Label6.Text = "Cancelled"
            MessageBox.Show("Download Aborted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Label6.Text = "Download Successfully"
            MessageBox.Show("Donload Complated", "All Ok", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.ProgressBar1.Value = 0
        Me.Label2.Text = "Downloading :"
        Me.Label3.Text = "Safe to :"
        Me.Label4.Text = "File Size :"
        Me.Label5.Text = "Download Speed :"
        Me.Label6.Text = ""

    End Sub

Code Button Cancel

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.BackgroundWorker1.CancelAsync() ' its for send cancel request
        ' lets try it
    End Sub
Untuk keseluruhan Source code simple Download Manager menggunakan bahasa pemrograman vb.net silahkan copy code berikut :

Source code aplikasi Internet Download Manager lengkap

Imports System.Net
Public Class Form1
    Dim wheretosave As String ''Where the program save the file
    Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
    Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label6.Text = ""
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.TextBox1.Text <> "" AndAlso Me.TextBox1.Text.StartsWith("http://") Then
            'we will create the save file dialog here
            Me.SaveFileDialog1.FileName = Me.TextBox1.Text.Split("/"c)(Me.TextBox1.Text.Split("/"c).Length - 1)
            If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.wheretosave = Me.SaveFileDialog1.FileName
                Me.SaveFileDialog1.FileName = ""
                Label3.Text = "Save to : " & Me.wheretosave
                Me.TextBox1.Enabled = False
                Me.Button1.Enabled = False
                Me.Button2.Enabled = True
                Me.BackgroundWorker1.RunWorkerAsync() ' start our download
            End If
        Else
            MessageBox.Show("Warning : Please insert valid URL for download", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        ' creating the request and getting the response
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest

        Try 'check if the file is exist

            theRequest = WebRequest.Create(Me.TextBox1.Text)
            theResponse = theRequest.GetResponse

        Catch ex As Exception

            MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf &
                            "1) File doesn't exist" & ControlChars.CrLf &
                            "2) Remote server error", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            'we will create the delegate here
            ' just wait fo a moment

            Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
            Me.Invoke(cancelDelegate, True)
            Exit Sub
        End Try

        Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
        ' we will create the functions for update the informations
        ' just wait for a moment

        Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
        Me.Invoke(safedelegate, lenght, 0, 0, 0)

        Dim writestream As New IO.FileStream(Me.wheretosave, IO.FileMode.Create)

        'Replacement for Stream.Position (webResponse stream doesn't support seek)
        Dim nRead As Integer

        'To calculate the download speed
        Dim speedTimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0

        Do
            If BackgroundWorker1.CancellationPending Then 'If user abort download
                Exit Do
            End If
            speedTimer.Start()
            Dim readBytes(4095) As Byte
            Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
            nRead += bytesread

            Dim percent As Short = (nRead * 100) / lenght
            Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)

            ' sorry for it, just replace the variable speed to double
            ' lets try it again

            If bytesread = 0 Then Exit Do

            writestream.Write(readBytes, 0, bytesread)
            speedTimer.Stop()

            readings += 1
            If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles

                currentspeed = 20480 / (speedTimer.ElapsedMilliseconds / 1000)
                speedTimer.Reset()
                readings = 0
            End If

        Loop

        'Close the streams
        theResponse.GetResponseStream.Close()
        writestream.Close()

        If Me.BackgroundWorker1.CancellationPending Then

            IO.File.Delete(Me.wheretosave)
            Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
            Me.Invoke(canceldelegate, True)
            Exit Sub
        End If

        Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
        Me.Invoke(complatedelegate, False)
    End Sub

    Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
        Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
        Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "

        If speed = -1 Then

            Me.Label5.Text = "Speed : Calculating ..."
        Else
            Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent

    End Sub
    Public Sub DownloadComplate(ByVal cancelled As Boolean)

        Me.TextBox1.Enabled = True
        Me.Button1.Enabled = True
        Me.Button2.Enabled = False

        If cancelled Then

            Label6.Text = "Cancelled"
            MessageBox.Show("Download Aborted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Label6.Text = "Download Successfully"
            MessageBox.Show("Donload Complated", "All Ok", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.ProgressBar1.Value = 0
        Me.Label2.Text = "Downloading :"
        Me.Label3.Text = "Safe to :"
        Me.Label4.Text = "File Size :"
        Me.Label5.Text = "Download Speed :"
        Me.Label6.Text = ""

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.BackgroundWorker1.CancelAsync() ' its for send cancel request
        ' lets try it
    End Sub
End Class
Aplikasi Download manager diatas dikhususkan hanya untuk kamu yang masih pemula dalam belajar bahasa pemrograman visual basic net. jika masih bingung bisa melihat video tutorialnya langsung di youtube atau dibawah ini :

Video tutorial Cara membuat Aplikasi Download Manager


Google Plus : https://plus.google.com/+HarisonMatondang
Youtube : http://www.youtube.com/c/HarisonMatondang
Officel Website : Belajar membuat game android sampai ngehh di ngehh.id

Jangan lupa share dan subscribe juga ya untuk mendapatkan tips dan trik serta tutorial programming terbaru setiap harinya.

Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: VB.NET Pemula : Membuat Internet Download Manager + BackgroundWorker + ProgressBar
VB.NET Pemula : Membuat Internet Download Manager + BackgroundWorker + ProgressBar
Belajar Bahasa pemrograman VB.NET pemula : cara membuat Internet Download Manager lengkap dengan informasi download dan Background worker serta progressbar di visual studio 2015
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjm2VTCFyZPbJunbb4b6bMfQEzetNP-Lgzate1y1phT7NfASvARJuTvQxGnAoe4fO6wya63HZHPv-sCyJ6U2BI0I_87TBU057869I8uAbWKmnDHG2bkNKSoaK6OiU-ry_TGUiJvn03h92o/s320/aplikasi-download-manager-vb-net.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjm2VTCFyZPbJunbb4b6bMfQEzetNP-Lgzate1y1phT7NfASvARJuTvQxGnAoe4fO6wya63HZHPv-sCyJ6U2BI0I_87TBU057869I8uAbWKmnDHG2bkNKSoaK6OiU-ry_TGUiJvn03h92o/s72-c/aplikasi-download-manager-vb-net.jpg
KODE AJAIB
https://scqq.blogspot.com/2016/04/vbnet-membuat-internet-download-manager.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/04/vbnet-membuat-internet-download-manager.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy