Thread throw System.OutOfMemoryException in a WPF Application

The following code will call another thread when the processing application shows progress windows. It will throw an exception after we make multiple times, for example, hit more than 50 times. This is our code - BusyIndicatorHelper.ShowProgWindowCustomSize as selected from the exception and called by 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 shown below.

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

The following is an outofmemory exception.

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

- VerifyFinger, .

BusyIndicatorHelper busyIndicatorHelper = new BusyIndicatorHelper();
List<WorkerDO> docList = new         
DatabaseHelper().SearchDocInfo(UserContext.VdrInfo.WorkerObj.WrkrId);

if (docList != null && docList.Count > 0)
{   busyIndicatorHelper.ShowBusyIndicatorCustomSize("Verification",  
    WindowSetting.GetCurrentWindowState(this));

    FingerPrintHelper fp = null;
    if (_fpHelper != null)
       fp = _fpHelper;
    else
       fp = FingerPrintHelper.GetFingerPrinterHelperObj;

    verifyStatus = fp.VerifyFinger(docList, _viewModel.DetectedFingers,  
    IsIndexFingerSelected);
    docList = null;
    _viewModel.DetectedFingers = null;
}
+4
3

. - WPF, - SDK, .

. .

0

CurrentDispatcher? , . , BusyWindow, (- 1MB ), , . , , .

- , ThreadPool Task Parallel Library. .

Update:
​​:

_viewModel.DetectedFingers = null;

, Image, . , null, Dispose() , :

verifyStatus = fp.VerifyFinger(docList, _viewModel.DetectedFingers,  
IsIndexFingerSelected);
docList = null;
_viewModel.DetectedFingers.Dispose();
_viewModel.DetectedFingers = null;
0

Each time you call this method, you create a new LoadingWindow object in a new thread.

progWindow = new LoadingWindow (_message);

Do you release the previous LoadingWindow file before creating a new one?

-1
source

Source: https://habr.com/ru/post/1626112/


All Articles