locked
count time to write file. RRS feed

  • Question

  • hello again, this time im creating a Hard drive tool that self generates bytes( 1 x 2 x 2 x 2 x 2 until it reaches 500MB) (To test harddrive speed)

    my issue is, when the timer ive made in form2.vb gets started, its automatically done. and my SSD hard drive does not have a 500MBPS write speed.

    here is my code:

     RichTextBox1.Rtf = RichTextBox1.Rtf.Replace("1", "11")
    ' THIS IS DUPLICATED TO GENERATE 500MB of BYTES
    'label 35 is how many bytes(1's) in richtextbox.
    'label 36 is how many seconds it took to finish the write.
            Label35.Text = RichTextBox1.Text.Count
            Dim oFile As System.IO.File
            Dim oWrite As System.IO.StreamWriter
            Form2.Show()
    'system.io is imported.
            oWrite = File.CreateText("C:\byte.txt")
            oWrite.Write(RichTextBox1.Text)
            Label36.Text = Form2.labelderp.Text
    
            oWrite.Close()
            Form2.Timer1.Stop()
    
            Label36.Text = Form2.labelderp.Text
    
            'TextBox1.Text = Label35.Text / form2.labelderp.text
            'ProgressBar1.Value = TextBox1.Text
    
    
    so, im kind of running out of ways too go.

    any help is appreciated, thanks!

    Wednesday, August 15, 2012 2:01 AM

Answers

  • Try This
    Imports System.IO
    Imports System
    Imports System.Diagnostics
    Imports System.Threading
    Public Class Form1
        Delegate Sub EnableButton(ByVal Button As Button)
        Delegate Sub UpdateLabel(ByVal Label As Label, ByVal text As String)
        Dim stopWatch As New Stopwatch()
        Sub updL(ByVal Label As Label, ByVal text As String)
            If Label.InvokeRequired Then
                Label.Invoke(New UpdateLabel(AddressOf updL), Label, text)
            Else
                Label.Text = text
                Label.Update()
            End If
        End Sub
        Sub enableB(ByVal Button As Button)
            If Button.InvokeRequired Then
                Button.Invoke(New EnableButton(AddressOf enableB), Button)
            Else
                Button.Enabled = True
            End If
        End Sub
        Function DoTest() As Long
            Dim Sw As Stopwatch = Nothing
            Try
                Dim Kilobyte As Integer = 1024
                Dim Megabyte As Integer = Kilobyte * 1024
                Dim MB500 As Integer = Megabyte * 500
                Dim TestFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & "testfile.dat"
                Dim Bytes(MB500 - 1) As Byte
                updL(Label35, "Preparing Test")
                For I As Long = 0 To MB500 - 1
                    Bytes(CInt(I)) = 255
                Next
                updL(Label35, "Test In Progress")
                Sw = stopWatch.StartNew
                My.Computer.FileSystem.WriteAllBytes(TestFile, Bytes, False)
                Sw.Stop()
                updL(Label35, "Test Complete")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Return Sw.ElapsedMilliseconds
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim Th As New Thread(AddressOf Threadwork)
            Th.Start()
        End Sub
        Delegate Sub TBUpdater(Textbox As TextBox, text As String)
        Delegate Sub PBUpd(ProgressBar As ProgressBar, Value As Integer)
        Sub updatePB(ProgressBar As ProgressBar, Value As Integer)
            If ProgressBar.InvokeRequired Then
                ProgressBar.Invoke(New PBUpd(AddressOf updatePB), ProgressBar, Value)
            Else
                ProgressBar.Value = Value
            End If
        End Sub
        Sub updateTB(Textbox As TextBox, text As String)
            If Textbox.InvokeRequired Then
                Textbox.Invoke(New TBUpdater(AddressOf updateTB), Textbox, text)
            Else
                Textbox.Text = text
            End If
        End Sub
        Sub Threadwork()
            Dim TS As TimeSpan = TimeSpan.FromMilliseconds(DoTest)
            enableB(Button1)
            updL(Label36, TS.Seconds.ToString & "sec")
            'TextBox1.Text = (500 / CInt(TS.Seconds))
            updateTB(TextBox1, (500 / CInt(TS.Seconds)))
            '  ProgressBar1.Value = TextBox1.Text
            updatePB(ProgressBar1, CInt(TextBox1.Text))
            'ts.seconds
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ProgressBar1.Maximum = 3000
            ProgressBar1.Minimum = 0
        End Sub
    End Class


    If you want something you've never had, you need to do something you've never done.



    • Marked as answer by Andrew Haughie Wednesday, August 15, 2012 3:40 AM
    • Edited by Paul Ishak Wednesday, August 15, 2012 3:22 PM
    Wednesday, August 15, 2012 3:36 AM

All replies

  • You don't want to use Timer for this.  Timer is meant to run code at specific intervals, not to measure time.

    If you want to measure time, you can do this via using a Stopwatch: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 15, 2012 2:05 AM
  • that was a bit helpful, but about the stopwatch, would i have to put it in a function? with diffrent calls? and if you can, could you explain a bit more on how to use this?(looks like a bit of a timer to me, so this could help, Thanks)

    edit: ive gotten into it a bit, but another issue i have is that i need it in seconds, is there a way to do this?

    Second edit:

    here is my new code:

    RichTextBox1.Rtf = RichTextBox1.Rtf.Replace("1", "11")
    
            
    
            Label35.Text = RichTextBox1.Text.Count
            Dim oFile As System.IO.File
            Dim oWrite As System.IO.StreamWriter
            stopWatch.start()
            Dim ts As TimeSpan = stopWatch.Elapsed
    
            ' Format and display the TimeSpan value.
            Dim elapsedTime As String = String.Format("{0}", ts.Seconds)
    
    
    
            oWrite = File.CreateText("C:\byte.txt")
            oWrite.Write(RichTextBox1.Text)
            Label36.Text = elapsedTime
    
    
            oWrite.Close()
    
    
            Label36.Text = elapsedTime
    
            TextBox1.Text = Label35.Text / Label36.Text
            ProgressBar1.Value = TextBox1.Text

    i now get an error at the last line: Arithmetic operation resulted in an overflow.

    what can i do to prevent this?

    Wednesday, August 15, 2012 2:08 AM
  • Try this sample.

    Option Strict On
    Imports System.Threading
    Public Class Form1
        Delegate Sub EnableButton(ByVal Button As Button)
        Delegate Sub UpdateLabel(ByVal Label As Label, ByVal text As String)
        Sub updL(ByVal Label As Label, ByVal text As String)
            If Label.InvokeRequired Then
                Label.Invoke(New UpdateLabel(AddressOf updL), Label, text)
            Else
                Label.Text = text
                Label.Update()
            End If
        End Sub

        Sub enableB(ByVal Button As Button)
            If Button.InvokeRequired Then
                Button.Invoke(New EnableButton(AddressOf enableB), Button)
            Else
                Button.Enabled = True
            End If
        End Sub
        Function DoTest() As Long
            Dim Sw As Stopwatch = Nothing
            Try
                Dim Kilobyte As Integer = 1024
                Dim Megabyte As Integer = Kilobyte * 1024
                Dim MB500 As Integer = Megabyte * 500
                Dim TestFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & "testfile.dat"
                Dim Bytes(MB500 - 1) As Byte
                updL(Label1, "Preparing Test")
                For I As Long = 0 To MB500 - 1
                    Bytes(CInt(I)) = 255
                Next
                updL(Label1, "Test In Progress")
                Sw = Stopwatch.StartNew
                My.Computer.FileSystem.WriteAllBytes(TestFile, Bytes, False)
                Sw.Stop()
                updL(Label1, "Test Complete")
                My.Computer.FileSystem.DeleteFile(TestFile)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Return Sw.ElapsedMilliseconds
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim Th As New Thread(AddressOf Threadwork)
            Th.Start()
        End Sub
        Sub Threadwork()
            Dim TS As TimeSpan = TimeSpan.FromMilliseconds(DoTest)
            enableB(Button1)
            MsgBox("Completed in " & TS.Days & "d" & TS.Hours & "h" & TS.Minutes & "m" & TS.Seconds & "s" & TS.Milliseconds & "ms")
        End Sub
    End Class



    If you want something you've never had, you need to do something you've never done.




    • Edited by Paul Ishak Wednesday, August 15, 2012 3:23 PM
    Wednesday, August 15, 2012 2:32 AM
  • Im getting somewhere!

    im auctly recieving seconds now.

    i going to tune it a small bit, and if it works, i will mark you as correct, and thanks for your help!

    edit: i look at the file properties, and its 32 MB for Size on disk, but it is reading 500MB at size.

    edit 2: how do i get it to divide the 500MB into the Seconds so i can get the result speeds?

    Wednesday, August 15, 2012 2:39 AM
  • It works fine for me....


    If you want something you've never had, you need to do something you've never done.

    Wednesday, August 15, 2012 2:44 AM
  • this makes no sense.

    Wednesday, August 15, 2012 2:47 AM
  • Do you have compression on your disc? I think you may...

    If you want something you've never had, you need to do something you've never done.


    • Edited by Paul Ishak Wednesday, August 15, 2012 2:49 AM
    Wednesday, August 15, 2012 2:48 AM
  • yeah, for selective folders though, thanks for reminding-ish in a way, ill undo the desktop.

    that solved that, just one more issue.

    when i change

      MsgBox("Completed in " & TS.Days & "d" & TS.Hours & "h" & TS.Minutes & "m" & TS.Seconds & "s" & TS.Milliseconds & "ms")

    into

     Label36.Text = TS.Seconds
    
            TextBox1.Text = 524800000 / Label36.Text
            ProgressBar1.Value = TextBox1.Text
    

    i get: 

    Cross-thread operation not valid: Control 'Label36' accessed from a thread other than the thread it was created on.

    after that, thats it, and thanks.

    Wednesday, August 15, 2012 2:49 AM
  • Since its multithreaded you cannot access the label directly(like this: Label.Text="text").

    You have to use the delegate, which I provided.

     Delegate Sub UpdateLabel(ByVal Label As Label, ByVal text As String)
        Sub updL(ByVal Label As Label, ByVal text As String)
            If Label.InvokeRequired Then
                Label.Invoke(New UpdateLabel(AddressOf updL), Label, text)
            Else
                Label.Text = text
                Label.Update()
            End If
        End Sub

          updL(Label36, "Your Text")


    If you want something you've never had, you need to do something you've never done.


    • Edited by Paul Ishak Wednesday, August 15, 2012 3:02 AM
    Wednesday, August 15, 2012 3:01 AM
  • sorry to keep going on and on, but now i have this:


    updL(Label36, TS.Seconds)
            TextBox1.Text.Equals(524800000 / TS.Seconds)
            ProgressBar1.Value = TextBox1.Text

    and there is no error, but nothing shows up in textbox1.

    running WRITE test
    Wednesday, August 15, 2012 3:12 AM
  • Upload your project, I'll take a look at it


    If you want something you've never had, you need to do something you've never done.

    Wednesday, August 15, 2012 3:13 AM
  • appreciate it,

    Wednesday, August 15, 2012 3:16 AM
  • Try This
    Imports System.IO
    Imports System
    Imports System.Diagnostics
    Imports System.Threading
    Public Class Form1
        Delegate Sub EnableButton(ByVal Button As Button)
        Delegate Sub UpdateLabel(ByVal Label As Label, ByVal text As String)
        Dim stopWatch As New Stopwatch()
        Sub updL(ByVal Label As Label, ByVal text As String)
            If Label.InvokeRequired Then
                Label.Invoke(New UpdateLabel(AddressOf updL), Label, text)
            Else
                Label.Text = text
                Label.Update()
            End If
        End Sub
        Sub enableB(ByVal Button As Button)
            If Button.InvokeRequired Then
                Button.Invoke(New EnableButton(AddressOf enableB), Button)
            Else
                Button.Enabled = True
            End If
        End Sub
        Function DoTest() As Long
            Dim Sw As Stopwatch = Nothing
            Try
                Dim Kilobyte As Integer = 1024
                Dim Megabyte As Integer = Kilobyte * 1024
                Dim MB500 As Integer = Megabyte * 500
                Dim TestFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & "testfile.dat"
                Dim Bytes(MB500 - 1) As Byte
                updL(Label35, "Preparing Test")
                For I As Long = 0 To MB500 - 1
                    Bytes(CInt(I)) = 255
                Next
                updL(Label35, "Test In Progress")
                Sw = stopWatch.StartNew
                My.Computer.FileSystem.WriteAllBytes(TestFile, Bytes, False)
                Sw.Stop()
                updL(Label35, "Test Complete")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Return Sw.ElapsedMilliseconds
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim Th As New Thread(AddressOf Threadwork)
            Th.Start()
        End Sub
        Delegate Sub TBUpdater(Textbox As TextBox, text As String)
        Delegate Sub PBUpd(ProgressBar As ProgressBar, Value As Integer)
        Sub updatePB(ProgressBar As ProgressBar, Value As Integer)
            If ProgressBar.InvokeRequired Then
                ProgressBar.Invoke(New PBUpd(AddressOf updatePB), ProgressBar, Value)
            Else
                ProgressBar.Value = Value
            End If
        End Sub
        Sub updateTB(Textbox As TextBox, text As String)
            If Textbox.InvokeRequired Then
                Textbox.Invoke(New TBUpdater(AddressOf updateTB), Textbox, text)
            Else
                Textbox.Text = text
            End If
        End Sub
        Sub Threadwork()
            Dim TS As TimeSpan = TimeSpan.FromMilliseconds(DoTest)
            enableB(Button1)
            updL(Label36, TS.Seconds.ToString & "sec")
            'TextBox1.Text = (500 / CInt(TS.Seconds))
            updateTB(TextBox1, (500 / CInt(TS.Seconds)))
            '  ProgressBar1.Value = TextBox1.Text
            updatePB(ProgressBar1, CInt(TextBox1.Text))
            'ts.seconds
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ProgressBar1.Maximum = 3000
            ProgressBar1.Minimum = 0
        End Sub
    End Class


    If you want something you've never had, you need to do something you've never done.



    • Marked as answer by Andrew Haughie Wednesday, August 15, 2012 3:40 AM
    • Edited by Paul Ishak Wednesday, August 15, 2012 3:22 PM
    Wednesday, August 15, 2012 3:36 AM

  • If you want something you've never had, you need to do something you've never done.

    Wednesday, August 15, 2012 3:39 AM
  • thats pretty fudgeing accurate, and thanks. my SSD says it can hit up to 275 MBPS write. hit 250.

    Big thanks man.

    Wednesday, August 15, 2012 3:40 AM
  • no problem ;)

    If you want something you've never had, you need to do something you've never done.

    Wednesday, August 15, 2012 3:41 AM