读入文件导致 C# 中出现 StackOverflowException

发布于 2024-12-02 12:32:26 字数 875 浏览 2 评论 0原文

我目前正在开发一个 C# wpf 项目。这些文件是从数据库中以 SQL 语句的形式导出的数据。对于文件中读取的每一行,它都会执行语句以对 MySQL 服务器内的数据库执行操作。

该任务在报告进度的后台工作程序中运行,因此每次在 MySQL 服务器上执行一行时,它都会调用进度更改事件来更新进度条,以便用户知道程序已恢复到服务器的程度。

但是,当到达某个点时,进度更改事件会因堆栈溢出异常而崩溃。下面是更新进度条的代码,这是发生 stackoverflow 异常的地方。

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);
}

我该如何解决这个异常?我尝试运行垃圾收集器,但我认为这不会影响堆栈,因此它不起作用,我也尝试为每行读取添加一个小延迟,但这似乎也没有帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-12-09 12:32:26

您不需要 window.Dispatcher.Invoke,因为 BackgroundWorker 已经将对 ProgressChanged 的调用封送回其原始 SynchronizationContext< /代码>。 (这是假设您从 UI 线程启动 BW...)

尝试删除它,然后直接设置您的值:

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     progressRestore.IsIndeterminate = false;
     progressRestore.Value = e.ProgressPercentage;
     lblRestProgress.Content = e.ProgressPercentage + "%";
     //lblRestProgressDesc.Content = "Row " + lineCount + " of " + totalRows;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文