在错误的线程上访问应用程序数据 [英] Accessing Application Data on wrong Thread

查看:62
本文介绍了在错误的线程上访问应用程序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试从网站上保存地址(屏幕抓取)并将其保存到数据库中. WebRequest对象现在似乎仅异步支持此功能,这似乎导致我出现线程问题...您可以看到下面突出显示的行 提示我无法访问当前线程上的DataWorkspace.

I'm trying to save addresses from a website (screen scraping) and save them to the database. The WebRequest object only seems to support this asynchronously now, which seems to be causing me threading problems...you can see the highlighted line below gives me a message that I can't access the DataWorkspace on the current thread.

我对线程模型的工作原理了解有限,这似乎一直困扰着我.有没有推荐的资源可以用简单的术语真正地解释这些内容?关于如何修复以下代码的任何建议?

My limited knowledge around how the threading model works seems to keep tripping me up with these types of issues. Is there any recommended resources that really explain this stuff in simple terms? Any suggestions on how I might fix the code below?

谢谢!


上面突出显示的行给出了以下错误:

The above hilighted line gives the error below:

推荐答案

本质上,您需要做的是使用调度程序BeginInvoke调用来包围可为您提供错误的代码.

Essentially, what you'll need to do is surround the code that gives you the error with a dispatcher BeginInvoke call.

此线程中有深入讨论,

There's an in-depth discussion in this thread, Why am I getting an InvalidOperationException when trying to access an entity's child collection?, where the team's Justin Anderson explains about how threading works in LightSwitch (you'll just have to read through all the "other" stuff to get there).


这篇关于在错误的线程上访问应用程序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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