我有以下代码:
var queue = printer.PrintQueue;
var canPrint = ! Dispatcher.CurrentDispatcher.Invoke(()
=> queue.IsPaperJammed || queue.IsOutOfPaper ||
queue.IsInError || queue.HasPaperProblem);
它抛出以下错误:
The calling thread cannot access this object because a different thread owns it
我已经在 UI 线程上试过了(使用如上所示的调度器),我已经在当前线程上试过了(没有调度器)。
有没有办法询问哪个线程拥有它的对象?
最佳答案
你试过不使用 CurrentDispatcher
吗? :
var canPrint = ! Application.Current.Dispatcher.Invoke(()
=> queue.IsPaperJammed || queue.IsOutOfPaper ||
queue.IsInError || queue.HasPaperProblem);
CurrentDispatcher.Invoke()
将从当前执行的线程调用您的代码,它是非 UI 线程,假设此问题中的片段是从非 UI 线程运行的。
引用资料:
关于c# - 非用户界面 "The calling thread cannot access this object because a different thread owns it",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211845/