这段代码抛出一个System.UnauthorizedAccessException: Invalid cross-thread access exception。
private void DoWorker(object sender,DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
using (DbContext db = new DbContext(DbContext.littreConnectionString))
{
if (db.DatabaseExists())
{
var searchResults = from dbObject in db.LDicTable
where dbObject.word == SearchBox.Text
select dbObject;
if (!searchResults.Any())
{
wordExists = false;
}
}
}
}发布于 2014-11-20 01:59:14
这是因为你不能在后台worker dowork方法中访问UI线程对象,因为它在单独的线程中执行,而不是在UI thread.so中,它在不同的线程中执行,而您试图访问UI线程对象。
您必须将文本框文本存储在某个字符串中,并在方法中使用该变量。
有关详细信息,请参阅以下链接:
http://www.codeproject.com/Questions/623667/BackGroundWorker-Thread-issue-with-UI-Thread
Why can't UI components be accessed from a backgroundworker?
https://stackoverflow.com/questions/27023432
复制相似问题