WPF应用程序中的线程抛出System.OutOfMemoryException [英] Thread throw System.OutOfMemoryException in WPF application

查看:131
本文介绍了WPF应用程序中的线程抛出System.OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序执行处理以显示进度窗口时,以下代码将调用另一个线程。在我们多次执行后会抛出异常,例如命中次数超过50次。

这是我们的代码 - 从异常抛出的BusyIndi​​catorHelper.ShowProgWindowCustomSize并将调用以下代码。

The following code will call another thread when the application doing the processing to show the progress windows. It will throw an exception after we do multiple time, for example hit more than 50 times.
This is our code - BusyIndicatorHelper.ShowProgWindowCustomSize as thrown from the exception and will call the following code.

public void ShowBusyIndicatorCustomSize(string message, WindowCustom currentWindow, bool fileTransferStatus = false)
        {
            _message = message;
            using (_progressWindowWaitHandle = new AutoResetEvent(false))
            {
                _transferLoadVisibility = fileTransferStatus;
                //Starts the progress window thread
                Thread newprogWindowThread = new Thread(() => ShowProgWindowCustomSize(currentWindow));  //new Thread(new ThreadStart(ShowProgWindowNew(height, width, left, right)));
                newprogWindowThread.SetApartmentState(ApartmentState.STA);
                newprogWindowThread.IsBackground = true;
                newprogWindowThread.Start();

                //Wait for thread to notify that it has created the window
                _progressWindowWaitHandle.WaitOne();
                _isActive = true;
            }

        }

This will call ShowProgWindowCustomSize(currentWindow) as in the following.

private void ShowProgWindowCustomSize(WindowCustom currentWindow)
        {
            if (_transferLoadVisibility)
            {
                //creates and shows the progress window
                progWindow = new LoadingWindow(_message);
                progWindow.Height = currentWindow.WindowHeight;
                progWindow.Width = currentWindow.WindowWidth;
                progWindow.Left = currentWindow.WindowLeft;
                progWindow.Top = currentWindow.WindowTop;
                progWindow.WindowState = currentWindow.WindowState;

                progWindow.FileTansfer();
                progWindow.Show();
            }
            else
            {
                //creates and shows the progress window
                progWindow = new LoadingWindow(_message);
                progWindow.Height = currentWindow.WindowHeight;
                progWindow.Width = currentWindow.WindowWidth;
                progWindow.Left = currentWindow.WindowLeft;
                progWindow.Top = currentWindow.WindowTop;
                progWindow.WindowState = currentWindow.WindowState;
                progWindow.Show();
            }

            //makes sure dispatcher is shut down when the window is closed
            progWindow.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);

            //Notifies command thread the window has been created
            _progressWindowWaitHandle.Set();

            //Starts window dispatcher
            System.Windows.Threading.Dispatcher.Run();
        }







Application: BioMedicalVerification.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
Stack:
   at System.Windows.Media.Composition.DUCE+Channel.SyncFlush()
   at System.Windows.Media.MediaContext.CompleteRender()
   at System.Windows.Interop.HwndTarget.OnResize()
   at System.Windows.Interop.HwndTarget.HandleMessage(MS.Internal.Interop.WindowMessage, IntPtr, IntPtr)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.SetWindowPos(System.Runtime.InteropServices.HandleRef, System.Runtime.InteropServices.HandleRef, Int32, Int32, Int32, Int32, Int32)
   at System.Windows.Window.SetupInitialState(Double, Double, Double, Double)
   at System.Windows.Window.CreateSourceWindow(Boolean)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(System.Object)
   at System.Windows.Window.Show()
   <big>at Org.Bestinet.BV.Presentation.UI.BusyIndicatorHelper.ShowProgWindowCustomSize(Org.Bestinet.BV.Presentation.UI.WindowCustom)
   at Org.Bestinet.BV.Presentation.UI.BusyIndicatorHelper+<>c__DisplayClass2.<ShowBusyIndicatorCustomSize>b__0()</big>
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

推荐答案

基于错误stack我会检查你是否在某个时刻关闭了窗口并且被调用的线程结束了工作。根据您提供的代码,您可以在每个线程调用上显示一个新窗口,并且从堆栈中可以看到当窗口显示时堆栈溢出恰好发生在那里。因此,以前打开的窗口很可能仍然存在。
Based on the error stack I would check that you actually close the window at some point and the thread being called ends working. Based on the code you have provided, you show a new window on each thread call and from the stack you can see the the stack overflow happens exactly there, when the window is shown. So very likely the previously opened windows are still existing.


这篇关于WPF应用程序中的线程抛出System.OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆