لا اعلم خجم الملف الذي تتحدث عنه لكن أنصحك تستخدم Task بدلا من Backgroundworker
خاصة أن Backgroundworker أصبح موضه قديمة او أصبح تكنولوجيا قديمة
أيضا يجب عليك حين تحفظ البيانات
ان تفكر في تحويل كل شئ الي مصفوفة بايت
لنفترض مثلا ان مدخلات كل صف لديك مكوناتها كالأتي
اسم العميل
التاريخ
رقم مسلسل
الأفضل لك أن تكتبها علي هيئة Structure كالأتي
PHP كود :
Public Structure Customer
Public Property AccessTime As DateTime
Public Property Name As String
Public Property ID As Integer
End Structure
الأفضل لك تقوم بتحويل Structure الي مصفوفة بايت باستخدام الدالة التالية
PHP كود :
Private Function ToBytes(Of T As Structure)(value As T) As Byte()
Dim length As Integer = System.Runtime.InteropServices.Marshal.SizeOf(value)
Dim bytes As Byte() = New Byte(length - 1) {}
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
System.Runtime.InteropServices.Marshal.StructureToPtr(value, gc.AddrOfPinnedObject(), True)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return bytes
End Function
التحويل كالأتي
هناك مميزات اضافية في هذا الأسلوب هو انك تستطيع التخكم في مصفوفة البايت بأن تشفرها أو إن تضغطها
PHP كود :
Dim c As Customer = New Customer With {.AccessTime = DateTime.Now, .ID = 12345, .Name = "Omar amin"}
Dim values As Byte() = ToBytes(Of Customer)(c)
وتخفظ فقط مصفوفة البايت في ملف الداتا بيز
ولكي تسترجع البيانات الي حالتها الأصلية
تقرأ مصفوفة البايت من ملف الداتا بيز
ثم تحولها الي Structure
باستخدام الدالة التالية
PHP كود :
Private Function ToStructure(Of T As Structure)(bytes As Byte()) As T
Dim result As T = Nothing
Dim gc As System.Runtime.InteropServices.GCHandle = Nothing
Try
gc = System.Runtime.InteropServices.GCHandle.Alloc(bytes, System.Runtime.InteropServices.GCHandleType.Pinned)
result = CType((CObj(System.Runtime.InteropServices.Marshal.PtrToStructure(gc.AddrOfPinnedObject(), GetType(T)))), T)
Catch ex As Exception
Finally
If gc.IsAllocated Then
gc.Free()
End If
End Try
Return result
End Function
مثلا لتقرا سطرا واحدا من ملف الداتا بييز
مصفوفة البايت التي تم تعريفها في الكود أدناه باسم result هي التي يجب قرائتها من الداتا بيز
PHP كود :
Dim result As Byte() = Nothing
If result IsNot Nothing Then
FileOperations.DeleteFile(dName, fName, fExt)
Dim cust As Customer = ToStructure(Of Customer)(result)
Label1.Text = cust.AccessTime.ToString
Label2.Text = cust.Name
Label3.Text = cust.ID
End If
عموم كلما تضخم الملف عليك أن تفكر في أساليب مختلفة لحفظ البيانات
وغالبا لن تجد تكنولوجيا سريعة بما يكفي لقراءة ملفات ضخمة خاصة أني كل طريقه ولها عيوبها أو لها مشاكلها
الأفضل هو تغيير تفكيرك في كيفية حفظ البيانات و تحول كل شئ الي مصفوفة بايت فهذا يقلل من حجم الملفات
فقط Thimk Bytes