I'm reading the binary file using file stream and I'm using async-await in order to not freeze the UI
Await Task.Run(Sub()
For i = 0 To file_len Step 512
fs.Seek(i, SeekOrigin.Begin) ' look into the next 4096 of the file
fs.Read(array_for_fat, 0, 512) ' read it to the buffer
label2.text = i 'getting the error here
Next
End Sub)
the problem is that I cant update ( I want to update the info on how much the app already have read )
label2.text = i
from this thread
any solution please, how to update the textbox from the async-await sub or maybe I can use something better that fits my purpose? full code sample:
Private Async Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
OpenFileDialog1.FileName = ".bin file"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Label9.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
Else
Exit Sub
End If
file_len = New System.IO.FileInfo(Label9.Text).Length.ToString
If Label9.Text.Contains(".bin") Or Label9.Text.Contains(".BIN") Then
Else
Label9.Text = ""
End If
Using fs As New FileStream(Label9.Text, FileMode.Open, FileAccess.Read, FileShare.None)
Await Task.Run(Sub()
For i = 0 To file_len Step 512
fs.Seek(i, SeekOrigin.Begin) ' look into the next 4096 of the file
fs.Read(array_for_fat, 0, 512) ' read it to the buffer
For Each position In Form1.Locate(array_for_fat, bytesToFind)
If array_for_fat(0) <> 1 Or array_for_fat(52) <> &H41 Then Continue For
'searching for proper data in the 512byte dump from file
i = file_len 'stop reading the file when found the dump we need
For b = 28 To 100 Step 2
If array_for_fat(b) = 0 Then ' found? execute
Exit For
End If
result &= array_for_fat(b).ToString("X") & array_for_fat(b + 1).ToString("X")
Next
Exit For
Next
Next
End Sub)
end using
end sub
And the Locate sub code ( searching the bytes patter in the bytes array):
Public Function Locate(ByVal self As Byte(), ByVal candidate As Byte()) As Integer()
If IsEmptyLocate(self, candidate) Then Return Empty
Dim list = New List(Of Integer)()
For i As Integer = 0 To self.Length - 1
If Not IsMatch(self, i, candidate) Then Continue For
list.Add(i)
Next
Return If(list.Count = 0, Empty, list.ToArray())
End Function
Public Function IsEmptyLocate(ByVal array As Byte(), ByVal candidate As
Byte())
As Boolean
Return array Is Nothing OrElse candidate Is Nothing OrElse array.Length =
0 OrElse candidate.Length = 0 OrElse candidate.Length > array.Length
End Function
Public Function IsMatch(ByVal array As Byte(), ByVal position As Integer,
ByVal candidate As Byte()) As Boolean
If candidate.Length > (array.Length - position) Then Return False
For i As Integer = 0 To candidate.Length - 1
If array(position + i) <> candidate(i) Then Return False
Next
Return True
End Function