我怎样才能解决我的C#/ MVVM应用莫名ObjectDisposedExceptions? [英] How can I resolve inexplicable ObjectDisposedExceptions in my C# / MVVM application?

查看:216
本文介绍了我怎样才能解决我的C#/ MVVM应用莫名ObjectDisposedExceptions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了我的第一个MVVM应用程序。当我关闭应用程序,我经常用的ObjectDisposedException得到一个崩溃的原因。作为应用去世,应用程序窗口消失后立即出现崩溃。

I've written my first MVVM application. When I close the application, I often get a crash cause by an ObjectDisposedException. The crash appears as the application dies, just after the app window disappears.

得到一个堆栈跟踪很困难(的看我的其他问题),但最后我做到了,发现我的异常堆栈完全包含C#库(KERNEL32!BaseThreadStart,mscorwks!线程,mscorwks!WKS等)之内。

Getting a stacktrace was difficult (see my other question), but finally I did, and found that my stacktrace is entirely contained within C# libraries (kernel32!BaseThreadStart, mscorwks!Thread, mscorwks!WKS, etc).

此外,这种崩溃是不一致的。我最后结账并重建后,它停止发生......一小会儿。然后,它回来了。一旦开始发生,它持续发生,即使我清洁和重建。但擦拭和结帐有时使得它停一停

Furthermore, this crash is inconsistent. After my last checkout and rebuild, it stopped happening... for a little while. Then it came back. Once it starts happening, it keeps happening, even if I "Clean" and rebuild. But a wipe-and-checkout sometimes makes it stop for a while.

什么,我认为正在发生的事情:

我觉得我的处理时的ViewModels的GarbageCollector做一些有趣的事情。我ViewModelBase类的析构具有的WriteLine登录时调用析构函数,而我4的ViewModels,只有2或3得到处置,它似乎取决于结帐(例如,当我对矿井运行它,我看到一个一致的重复序列,但我的同事看到与设置不同的对象)不同的序列。

I think the GarbageCollector is doing something funny when disposing my ViewModels. My ViewModelBase class destructor has a WriteLine to log when the destructor is called, and of my 4 ViewModels, only 2 or 3 get disposed, and it seems to vary depending on checkout (e.g. when I run it on mine, I see a consistently repeated sequence, but my colleague sees a different sequence with different objects disposed).

由于堆栈跟踪有没有在里面我的代码的电话,我想这意味着它不是我的的代码的调用一个释放的对象的方法。所以这让我思考CLR是哑巴。

Since the stacktrace has none of my code's calls in it, I think that means that it's not my code that's calling a disposed object's method. So that leaves me thinking the CLR is being dumb.

这是否有道理?有一些方法可以让我得到的GC是一致的?这是一个红鲱鱼

Does this make sense? Is there some way I can get the GC to be consistent? Is this a red herring?

其他细节,可以帮助:

我所有的意见和的ViewModels的创建在我App.xaml.cs文件的应用程序的启动事件处理程序。同样的处理程序分配的ViewModels到DataContexts。我不知道这是否是正确的做法MVVM(正如我所说,我的第一个应用程序MVVM),但我不明白为什么会引起不良行为。

Other details that might help:
All of my Views and ViewModels are created in my App.xaml.cs file's Application's Startup event handler. That same handler assigns ViewModels to DataContexts. I'm not sure if this is correct MVVM practice (as I said, my first MVVM app), but I don't see why it would cause bad behavior.

如果需要的话我可以粘贴代码。

I can paste code if necessary.

推荐答案

您的应用程序抛出一个异常,因为视图模型破坏你的日志的动作还没有完成时你的主要应用程序退出。

Your application is throwing an exception because your logging action on ViewModel destruction hasn't completed when your main application exits.

您会发现,为了执行实际文件写入一个子进程产生。如果这不是你的主要的应用程序已经退出,那么你会得到一个错误的时间内完成。

You'll find that in order to perform the actual file writing a child process is spawned. If this hasn't completed by the time your main application has exited then you'll get an error.

如果你要那么执行这种类型的操作,您需要主应用程序等待期间所有的子进程/线程池,线程等来完成它退出前。

If you're going to perform this type of action then you need your main application to wait for a period for any child processes/threadpool threads etc. to complete before it exits.

如果您希望确保您可以登录事件您的应用程序关闭期间发生的话,我建议你运行你的日志记录过程(实际写入日志文件)作为您邮件投递到一个单独的主线程。这样,在你的记录过程已完成写入磁盘你的应用程序可以关闭。

If you wish to ensure that you can log events that occur during your application closure then I would suggest that you run your logging process (the actual writing to your log file) as a separate primary thread that you post messages to. That way your application can close before your logging process has completed writing to disk.

这篇关于我怎样才能解决我的C#/ MVVM应用莫名ObjectDisposedExceptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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