Hack Forums
[.NET] Progress reporting - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Progress reporting (/showthread.php?tid=3310261)



Progress reporting - negerhek - Mar 7, 2013

Hi, I am trying to create a personal FTP file uploader. I want the backgroundworker to report the progress of the upload, but however it doesn't report at all.
Here's the code:
Code
Imports System
Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
Imports System.ComponentModel

Public Class Form1
'==details==
    dim host As String = "ftp://nine"
    dim username As String = "nine"
    dim password As String = "nine"
'==details==
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  ToolStripStatusLabel1.Text = "Idle"
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  OpenFileDialog1.Title = "Select your file"
  OpenFileDialog1.FileName = ""
  OpenFileDialog1.ShowDialog()
  TextBox1.Text = OpenFileDialog1.FileName.ToString
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  Dim req As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(host & System.IO.Path.GetFileName(OpenFileDialog1.FileName).ToString()), System.Net.FtpWebRequest)
  req.Credentials = New System.Net.NetworkCredential(username, password)
  req.Method = System.Net.WebRequestMethods.Ftp.UploadFile

  Dim bfile() As Byte = System.IO.File.ReadAllBytes(TextBox1.Text)

  Dim str As System.IO.Stream = req.GetRequestStream()
  ToolStripStatusLabel1.Text = "Uploading: " & System.IO.Path.GetFileName(OpenFileDialog1.FileName).ToString()
  str.Write(bfile, 0, bfile.Length)
  str.Close()
  str.Dispose()
    End Sub
    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
  ToolStripProgressBar1.Value = e.ProgressPercentage
  Label1.Text = e.ProgressPercentage.ToString()
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
  ToolStripStatusLabel1.Text = "Idle"
    End Sub
End Class

I also tried
Code
BackgroundWorker1.ReportProgress(prog)

but that only reported the progress once and returned 0% as progress.


* - Meerkat. - Mar 7, 2013

The way you're uploading the data doesn't have the ability to report progress.
You'll need to find a way to find out how many bytes you have sent already, then calculate how many % you've sent.
Then use the ReportProgress method to report progress.


* - ident - Mar 7, 2013

If you want to report progress then you have to be able to measure progress.

You need to read the file in blocks and then calculate how much you have already read as a proportion of the total file size. You can calculate that and report it to the user after reading each block.