Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, January 14, 2015 11:51 PM
I created a program to keep several folders in sync with their backups. The testing and copying is done with a background worker which reads a list of the files in the "original" folder, looks for the same file in the backup and skips if it is there or else copies it. It has been working just fine for months but suddenly I am seeing this error:
{"Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: chunkLength"}
at System.Text.StringBuilder.ToString()
at FileSynch.Form1.BGW_Copy_ProgressChanged(Object sender, ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)
the line of code that throws the error is : TxtResults.AppendText(SB.ToString)
I tried setting the StringBuilder with the Int32,Int32 overload to a high value, which did not help. I tried synclock which didn't help. I haven't any idea if the chunk_length can be changed, nothing I find says it can. Here are some of the details from one exception:
Actual SB.Length = 4492 (&H118C)
SB.Capacity = &H5E80
SB.m_chunk_length = &H118A
SB.m_chunk_chars {Length = 24192}
MaxChunkSize = &H1F40
MaxCapacity = &H7FFFFFFF
this is the backgroundworker code:
For Each filename As String In Filelist
Sourcename = Path.GetFullPath(filename).Replace(Source, "")
DestnName = Path.Combine(Destn, Sourcename.Remove(0, 1))
FilesScanned += 1
SB.Append(Now.ToLongTimeString & "-Checking " & Sourcename)
If BGW_Copy.CancellationPending Then
BGW_Copy.ReportProgress(-1) ' just reports "User Cancelled"
Exit Sub
End If
If File.Exists(DestnName) Then
SB.Append(" - Backup Exists")
FilesSkipped += 1
Else
Try
SW.Restart()
Directory.CreateDirectory(Path.GetDirectoryName(DestnName))
File.Copy(filename, DestnName, False)
SW.Stop()
FilesCopied += 1
FLength = My.Computer.FileSystem.GetFileInfo(filename).Length
BytesCopied += FLength
SB2.Append(filename & " " & FLength.ToString & " " & TotalBytes.ToString & vbNewLine)
SB.Append(" - Copied " & ShowFriendlyNumber(FLength) & " in " & ShowFriendlyTime(SW.ElapsedMilliseconds))
Catch ex As Exception
SB.Append(" - Copy Failed " & ex.Message)
End Try
End If
SB.Append(vbNewLine)
If FilesScanned Mod UpdateFreq = 0 Then
BGW_Copy.ReportProgress(FilesScanned)
SB.Clear() End If
If BGW_Copy.CancellationPending Then
BGW_Copy.ReportProgress(-1) ' this just reports "User Cancelled"
Exit Sub
End If
Next
Progress Changed Code (simplified):
TxtResults.AppendText(SB.ToString) ' error here
UpdateFreq comes from a NumericUpDown and defaults to 10
A typical line of output:
9:46:34 AM-Checking \My DVD.iso - Backup Exists
or this:
9:48:12 AM-Checking \My DVD.iso - Copied 2.77 GiB in 38.0 sec.
All replies (4)
Thursday, January 15, 2015 12:24 AM ✅Answered | 1 vote
Read another thread over at stackoverflow suggesting that you should pass stringbuilder.Tostring across threads instead of the stringbuilder itself... Have you tried this?
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles |
*This post does not reflect the opinion of Microsoft, or its employees.
Thursday, January 15, 2015 12:28 AM ✅Answered
TxtResults.AppendText(SB.ToString) ' error here
Try:
TxtResults.AppendText(SomecustomEventArgs.AlreadyConverteredResultFromStringbuilderDotToStringMethod)
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles |
*This post does not reflect the opinion of Microsoft, or its employees.
Thursday, January 15, 2015 5:04 AM
That did it, thanks very much Paul. As to why it caused that particular error, I have no idea and have little desire to dwell on it.
I did not realize that the Background Worker does not wait for ReportProgress to complete, now that I know that to be true, it makes sense since a lengthy ReportProgress would block the BGWorker thread.
Thursday, January 15, 2015 5:06 AM
That did it, thanks much Paul.
Awesome!
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles |
*This post does not reflect the opinion of Microsoft, or its employees.