简体   繁体   English

ObjectDisposedException:无法访问已处置的对象。 对象名称:“调度程序”

[英]ObjectDisposedException: Cannot access a disposed object. Object name: 'Dispatcher'

I'm getting an error: 我收到一个错误:

ObjectDisposedException: Cannot access a disposed object. ObjectDisposedException:无法访问已处置的对象。 Object name: 'Dispatcher'. 对象名称:“调度程序”。

My code in modelview: 我在模型视图中的代码:

public CultureEventViewModel()
{
    CultureEvents = new List<CultureEvent>();
    WebClient webClient = new WebClient();
    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
    webClient.DownloadStringAsync(new Uri("sampleuri"));
}

    public void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {  
        CultureEvents = JsonConvert.DeserializeObject<List<CultureEvent>>(e.Result);
    }

I observed that it returns no error when I delete a line in webClient_DownloadStringCompleted. 我观察到在删除webClient_DownloadStringCompleted中的行时它没有返回错误。 Any ideas or more code needed? 需要任何想法或更多代码吗?

The scope of webclient is limited to the public CultureEventViewModel() instantiation (in other words it can be garbage collected as soon as the object is instantiated. Because there is an outstanding asynchronous task being performed (DownloadStringAsync) the garbage collector can not collect your webClient object. Webclient的范围仅限于公共CultureEventViewModel()实例化(换句话说,可以在实例化对象后立即对其进行垃圾回收。因为有一个出色的异步任务正在执行(DownloadStringAsync),垃圾回收器无法收集您的webClient宾语。

Once the string has been downloaded the webClient is fair game and can be disposed of. 一旦下载了字符串,webClient就是公平的游戏,可以被处置。 To keep the web client you need to give it an existance outside the instantiation. 为了保留Web客户端,您需要在实例化之外为其提供存在性。

for example 例如

Class CultureEventViewModel
private WebClient webclient
public CultureEventViewModel()
{
    CultureEvents = new List<CultureEvent>();
    WebClient webClient = new WebClient();

... ...

but note that this will not dispose of the webclient instance untill the class instance is disposed of. 但是请注意,直到处理完该类实例后,才会处理此webclient实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SSH.NET [ObjectDisposedException:无法访问已处置的 object。 Object 名称:'Renci.SshNet.SshClient'。] - SSH.NET [ObjectDisposedException: Cannot access a disposed object. Object name: 'Renci.SshNet.SshClient'.] Owin:异常:System.ObjectDisposedException:无法访问已处置的 object。Object 名称:'System.Net.HttpListenerRequest' - Owin: Exception: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.HttpListenerRequest' System.ObjectDisposedException:无法访问已处理的对象。 对象名称:&#39;Syncfusion.ListView.XForms.Android.ExtendedScrollViewRenderer&#39; - System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Syncfusion.ListView.XForms.Android.ExtendedScrollViewRenderer' System.ObjectDisposedException:无法访问已处置的对象。 AplicationDbContext - System.ObjectDisposedException: Cannot access a disposed object. AplicationDbContext System.ObjectDisposedException:&#39;无法访问已处置的对象。&#39; - System.ObjectDisposedException: 'Cannot access a disposed object.' ObjectDisposedException-无法访问已处置的对象 - ObjectDisposedException - Cannot access a disposed object ObjectDisposedException:无法访问已处置的对象 - ObjectDisposedException: Cannot access a disposed object 获取 System.ObjectDisposedException:无法访问已处置的对象。 对象名称:“RSA”。 在身份服务器 6 中使用手动密钥管理时 - getting System.ObjectDisposedException: Cannot access a disposed object. Object name: 'RSA'. while using manual key management in identity server 6 Polly 重试:System.ObjectDisposedException:无法访问已处置的 object。Object 名称:'Flurl.Http.Content.FileContent' - Polly Retry : System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Flurl.Http.Content.FileContent' AutoMapper IValueResolver:无法访问已处理的对象。 对象名称:&#39;IServiceProvider&#39; - AutoMapper IValueResolver: Cannot access a disposed object. Object name: 'IServiceProvider'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM