Reading in a file causing a StackOverflowException in C#
I am currently working on a C# wpf project. The files are exported data from a database which are in the form of an SQL statement. For each line that the file reads in it executes the statement to perform the action on the database inside the MySQL server.
The task is run inside a background worker which reports progress, so each time a line has been executed on the MySQL Server it calls the progress changed event to update a progress bar so that the user known how much the program has restored to the server.
However, when it gets to a certain point the progress changed event crashes with a stack overflow exception. Below is the code that updates the progress bar which is where the stackoverflow exception occurrs.
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
window.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
{
progressRestore.IsIndeterminate = false;
progressRestore.Value = e.开发者_如何学运维ProgressPercentage;
lblRestProgress.Content = e.ProgressPercentage + "%";
//lblRestProgressDesc.Content = "Row " + lineCount + " of " + totalRows;
return null;
}), null);
}
How can I resolve this exception? I have tried to run the garbage collector but I don't think this affects the stack so it didn't work and I have also tried adding a small delay to each line read but this doesn't seem to have helped either.
You shouldn't need the window.Dispatcher.Invoke
, as BackgroundWorker
already marshals the calls to ProgressChanged
back to its original SynchronizationContext
. (This is assuming you're starting the BW from the UI thread...)
Try removing this, and just setting your values directly:
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressRestore.IsIndeterminate = false;
progressRestore.Value = e.ProgressPercentage;
lblRestProgress.Content = e.ProgressPercentage + "%";
//lblRestProgressDesc.Content = "Row " + lineCount + " of " + totalRows;
}
精彩评论