c# – WPF应用程序中的线程抛出System.OutOfMemoryException
当应用程序执行处理以显示进度窗口时,以下代码将调用另一个线程.在我们多次执行后会抛出异常,例如命中次数超过50次.
这是我们的代码 – 从异常抛出的BusyIndi??catorHelper.ShowProgWindowCustomSize并将调用以下代码. 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; } } 这将调用ShowProgWindowCustomSize(currentWindow),如下所示. 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(); } 以下是抛出的outofmemory异常.
我怀疑因为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; } 解决方法
为什么要关闭CurrentDispatcher?它是您程序中唯一的一个,您的代码永远不会执行关闭.因此,每次打开BusyWindow时,都会创建新线程(距离内存为1MB),并且它会进入无限循环,从而占用系统资源的另一部分.最终你的程序会从你的异常状态中脱离内存.
你真的不应该为你的任务开始一个新的线程 – 使用更高的抽象杠杆,可能是 更新: _viewModel.DetectedFingers = null; 我怀疑这是你从客户那里得到的 verifyStatus = fp.VerifyFinger(docList,IsIndexFingerSelected); docList = null; _viewModel.DetectedFingers.Dispose(); _viewModel.DetectedFingers = null; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |