|
-
Mar 8th, 2012, 03:06 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] BackGroundWorker freezing my app up
Hey Everyone,
I was messing around with the listview Control first,, And when i got it working i thought what's this backgroundworker thing,,so i read up on what info MS had on the MSDN page..and slapped it in here..and i thought i had it working ok until i tried to move my project application to the side to reply to a instant message i got..and then i noticed that it froze up .
and looked like it was using alot of system resources,
But wait let me back up just a bit.originally i was trying it with a fairly small number of items, and then i seen on here where someone had posted a rather large index for testing his app with,,and while trying to load that list that's when the issue was noticed 
i don't really plan on loading a list as big as that test index was but, I would still like to know why the program did what it did using the code below.
I'm not really asking for it to be too altered, i kinda like all the controls i have in it the moment,, and the way it loaded as well. but if someone could point me to the reason the app froze i would really appreciate it.
Thanks in Advance 
Code:
Imports System.IO
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Threading
Imports System.Collections
Imports System
Public Class Form1
Inherits Form
Private inhibitAutoCheck As Boolean
Dim listView1 As New ListView()
Dim Counter As Long, Total_Items As Long, File_Path As String, String_Array() As String
Private Delegate Sub Start_LoadingDelegate(ByVal parameter As Array)
Private Delegate Sub Begin_UpdateDelegate()
Dim Item As ListViewItem
Public Sub New()
InitializeComponent()
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
End Sub
Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)
TimeConsumingOperation(bw)
End Sub
Private Function TimeConsumingOperation(ByVal bw As BackgroundWorker) As Integer
Dim [exit] As Boolean = False
Dim Input_String() As String = IO.File.ReadAllLines(File_Path)
Total_Items = Input_String.GetUpperBound(0)
Begin_Update()
Try
For Me.Counter = 1 To Total_Items - 1
Dim String_Array() As String = Split(Input_String(Me.Counter), ",")
Start_Loading(String_Array)
BackgroundWorker1.ReportProgress(Counter * 100 / Total_Items)
If [exit] Then
Exit For
End If
Next
Catch
MsgBox("Problemo")
End Try
End Function
Public Sub Begin_Update()
If Not InvokeRequired Then
listView1.BeginUpdate()
Else
Invoke(New Begin_UpdateDelegate(AddressOf Begin_Update), New Object() {})
End If
End Sub
Public Sub Start_Loading(ByVal String_Array As Array)
If Not InvokeRequired Then
Item = listView1.Items.Add(String_Array(0))
Item.SubItems.Add(String_Array(1))
Item.SubItems.Add(String_Array(2))
Item.SubItems.Add(String_Array(3))
Item.SubItems.Add(String_Array(4))
Item.SubItems.Add(String_Array(5))
Item.SubItems.Add(String_Array(6))
Item.SubItems.Add(String_Array(7))
Item.SubItems.Add(String_Array(8))
Item.SubItems.Add(String_Array(9))
Else
Invoke(New Start_LoadingDelegate(AddressOf Start_Loading), New Object() {String_Array})
End If
End Sub
Private Sub backgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
resultLabel.Text = (e.ProgressPercentage.ToString() + "%")
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
resultLabel.Text = "Canceled!"
ElseIf (e.Error IsNot Nothing) Then
resultLabel.Text = "Error: " & e.Error.Message
Else
resultLabel.Text = "Done!"
listView1.EndUpdate()
End If
End Sub
Private Sub startButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startBtn.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
File_Path = openFileDialog1.FileName
Me.BackgroundWorker1.RunWorkerAsync()
End If
End Sub
Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cancelBtn.Click
Me.BackgroundWorker1.CancelAsync()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.listView1.Bounds = New Rectangle(New Point(12, 12), New Size(938, 418))
Me.listView1.Columns.Add("Column1", 200, HorizontalAlignment.Left)
Me.listView1.Columns.Add("Column2", 250, HorizontalAlignment.Left)
Me.listView1.Columns.Add("Column3", 100, HorizontalAlignment.Left)
Me.listView1.Columns.Add("Column4", 120, HorizontalAlignment.Left)
Me.listView1.Columns.Add("Column5", 100, HorizontalAlignment.Right)
Me.listView1.Columns.Add("Column6", 100, HorizontalAlignment.Left)
Me.listView1.Columns.Add("Column7", 120, HorizontalAlignment.Left)
listView1.View = View.Details
listView1.LabelEdit = False
listView1.AllowColumnReorder = True
listView1.CheckBoxes = True
listView1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right
Me.Controls.Add(listView1)
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|