Dispatcher 中抛出类型 'System.OutOfMemoryException' 的异常

Exception of type 'System.OutOfMemoryException' was thrown inside Dispatcher

请帮助我了解出了什么问题。我有一个进度条方法来启动 运行 任务中的进度条。 Base class 是静态 class,它保存来自另一个 class 的更新值。下面是我的代码:

private void startProgressBarBA()
    {
        int tempBA = 0;
        proBing.Maximum = Int32.Parse(cboGeoThreshold.Text);

        CancellationTokenSource bingBarToken = new CancellationTokenSource();
        Task bingTask = Task.Factory.StartNew(() =>
        {
            while (!bingBarToken.Token.IsCancellationRequested)
            {
                if (tempBA != Base.proBaValue)//Reduce the number of same value assigned.
                {
                    try
                    {
                        Dispatcher.BeginInvoke(new Action(() =>
                        {
                            if (proBing.Value == proBing.Maximum)
                            {
                                proBing.Value = 0;
                                Base.proBaValue = 0;
                            }
                            proBing.Value = Base.proBaValue;
                            tempBA = Base.proBaValue;
                            baRecord.Content = proBing.Value + "/" + proBing.Maximum;
                        }
                        ));
                    }
                    catch(OutOfMemoryException e)
                    {
                        throw e;
                    }
                }
                if (Base.checkTaskBA == false)
                {
                    bingBarToken.Cancel();
                }
            }
        },bingBarToken.Token, TaskCreationOptions.LongRunning,TaskScheduler.Default);
    }

一定时间后会出现异常,

Dispatcher.BeginInvoke

已突出显示。这是异常消息:

System.OutOfMemoryException was unhandled by user code
  HResult=-2147024882
  Message=Exception of type 'System.OutOfMemoryException' was thrown.
  Source=View
  StackTrace:
   at View.MainWindow.<>c__DisplayClass21_0.<startProgressBarBA>b__0() in C:\Wade\GeocodingApp\Geocoder_v21_Rev_pcode\View\MainWindow.xaml.cs:line 331
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
  InnerException: 

如何解决这个问题?是不是因为

new Action()

导致 OutOfMemoryException?感谢大家的帮助。

这里只是猜测,但看起来您在任务的 "while" 循环中没有延迟。

这意味着您正在快速循环该循环,并在 Dispatcher 队列上创建大量异步 "invoke" 消息 - 毫无疑问,这会耗尽所有内存.. ..它们的处理速度不够快,所以正在建立(即必须创建 100s thousands/maybe 数百万 "Action" 个对象)。

可能的解决方案....在您的 while 循环中放入某种 "wait",例如a Thread.Sleep(100) - 您不需要经常发送它们来指示进度。

private void startProgressBarBA()
{
    int tempBA = 0;
    proBing.Maximum = Int32.Parse(cboGeoThreshold.Text);

    CancellationTokenSource bingBarToken = new CancellationTokenSource();
    Task bingTask = Task.Factory.StartNew(() =>
    {
        while (!bingBarToken.Token.IsCancellationRequested)
        {
            if (tempBA != Base.proBaValue)//Reduce the number of same value assigned.
            {
                try
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (proBing.Value == proBing.Maximum)
                        {
                            proBing.Value = 0;
                            Base.proBaValue = 0;
                        }
                        proBing.Value = Base.proBaValue;
                        tempBA = Base.proBaValue;
                        baRecord.Content = proBing.Value + "/" + proBing.Maximum;
                    }
                    ));
                }
                catch(OutOfMemoryException e)
                {
                    throw e;
                }
            }
            if (Base.checkTaskBA == false)
            {
                bingBarToken.Cancel();
            }

            Thread.Sleep(100);
        }
    },bingBarToken.Token, TaskCreationOptions.LongRunning,TaskScheduler.Default);
}