Winform崩溃了! ,说不能访问处置对象,有什么建议吗? [英] Winform crashes! , Says cannot access disposed object, any suggestion?

查看:66
本文介绍了Winform崩溃了! ,说不能访问处置对象,有什么建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我创建了一个C#winform。但是当我运行它时,意外崩溃,有时说无法访问已处置的对象或值丢弃()。

我在程序中不处理对象。

它随机崩溃,我的意思是有时会崩溃,有时候不会崩溃!

它在不同的线程中更新控件。

我认为表单出乎意料地退出但我不知道为什么?$

我该怎么办?

如何找到问题?



什么我试过了:



我试过尝试捕获块但它没有捕获异常!

Hi there,
I have created a C# winform. But when I run it, crashes unexpectedly and sometimes says "cannot access a disposed object" or "value disposed()".
I don't dispose objects during my program.
It crashes randomly, I mean sometimes crashes and sometimes not!
It's updating controls in separate threads.
I think the form exits unexpectedly but I don't know why?!
What should I do?
How can I find the problem?

What I have tried:

I have tried try catch block but it doesn't catch the exception!

推荐答案

Disposed objects是您创建类的实例,保存对它的引用,然后Dispose原始对象的位置。当你尝试使用你保存的引用时,它就会崩溃,因为它引用的对象不再存在。

你可以直接这样做:
"Disposed objects" are where you create an instance of a class, save a reference to it, and then Dispose the original. When you try to use the reference you saved, it all falls apart, because the object it references no longer exists.
You could be doing this directly:
MyClass badIdea;
using (MyClass other = new MyClass())
   {
   badIdea = other;
   }
Console.WriteLine(badIdea);

或者更简单:

MyClass badIdea = new MyClass())
badIdea.Dispose();
Console.WriteLine(badIdea);

或间接:

MyClass badIdea;
using (MyOtherClass other = new MyOtherClass())
   {
   badIdea = other.ContainedInstanceOfMyClass;
   }
Console.WriteLine(badIdea);



我们不知道,我们无法分辨。因此,从调试器开始,找出它找到的处理时间和时间。然后开始回顾你的代码,找出它何时处理以及原因。


但是我们不能为你做任何事情:我们不要没有你的代码,我们没有你的数据,如果我们这样做,我们就不知道如何使用它!


We don't know, and we can't tell. So start with the debugger, and find out exactly what instance it is finding as disposed and when. Then start looking back in your code to find out when that was Disposed and why it was.

But we can't do any of that for you: we don't have your code, we don't have your data, and we wouldn't know how to use it if we did!


关闭一个C#表单(在大多数情况下)将调用Dispose 。这可能是被处置对象的来源。



在单独的线程中更新控件会导致InvalidOperationException。 (检查正在更新的控件上的InvokeRequired属性.Control.Invoke或Control.BeginInvoke可用于在创建控件的线程上调用方法。)
Closing a C# Form will (in most cases) call Dispose. This might be the source of objects being disposed.

Updating controls in separate threads can cause an InvalidOperationException. (Check the InvokeRequired property on the controls being updated. Control.Invoke or Control.BeginInvoke can be used to call a method on the thread that created the control.)


这篇关于Winform崩溃了! ,说不能访问处置对象,有什么建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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