“The calling thread cannot access this object because a different
thread owns it.”
问题在于:
writer.Write(fixeddocument.documentPaginator);
我无法获得dispatcher.invoke / begininvoke – 或其他任何东西来解决这个问题.
那么如何将另一个线程的Fixeddocument带到UI线程呢?
(请注意,Fixeddocument需要几分钟才能生成,因此必须在后台线程上创建.是的,我已经Google搜索了几个小时而没有找到任何解决方案.如果有,我错过了它).
我一直认为我必须将PrintPrevIEw保持在与Fixeddocument相同的线程中 – 我希望不会.
// Print PrevIEwpublic static voID PrintPrevIEw(Fixeddocument fixeddocument,CancellationToken ct){ // Was cancellation already requested? if (ct.IsCancellationRequested) ct.ThrowIfCancellationRequested(); MemoryStream ms = new MemoryStream(); using (Package p = Package.Open(ms,fileMode.Create,fileAccess.ReaDWrite)) { Uri u = new Uri("pack://TemporaryPackageUri.xps"); PackageStore.AddPackage(u,p); Xpsdocument doc = new Xpsdocument(p,Compressionoption.Maximum,u.absoluteUri); XpsdocumentWriter writer = Xpsdocument.CreateXpsdocumentWriter(doc); writer.Write(fixeddocument.documentPaginator); FixeddocumentSequence fixeddocumentSequence = doc.GetFixeddocumentSequence(); var prevIEwWindow = new PrintPrevIEw(fixeddocumentSequence); Action closeAction = () => prevIEwWindow.Close(); ct.Register(() => prevIEwWindow.dispatcher.Invoke(closeAction) ); prevIEwWindow.ShowDialog(); PackageStore.RemovePackage(u); doc.Close(); }}解决方法 它显然是你方法中的静态修饰符.将其更改为实例方法可以促进dispatcher:
public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); } // Print PrevIEw public voID PrintPrevIEw(Fixeddocument fixeddocument,CancellationToken ct) { // Was cancellation already requested? if (ct.IsCancellationRequested) ct.ThrowIfCancellationRequested(); MemoryStream ms = new MemoryStream(); using (Package p = Package.Open(ms,fileAccess.ReaDWrite)) { Uri u = new Uri("pack://TemporaryPackageUri.xps"); PackageStore.AddPackage(u,p); Xpsdocument doc = new Xpsdocument(p,u.absoluteUri); XpsdocumentWriter writer = Xpsdocument.CreateXpsdocumentWriter(doc); //writer.Write(fixeddocument.documentPaginator); dispatcher.Invoke(new Action<documentPaginator>(writer.Write),fixeddocument.documentPaginator); FixeddocumentSequence fixeddocumentSequence = doc.GetFixeddocumentSequence(); var prevIEwWindow = new PrintPrevIEw(fixeddocumentSequence); Action closeAction = () => prevIEwWindow.Close(); ct.Register(() => prevIEwWindow.dispatcher.Invoke(closeAction) ); prevIEwWindow.ShowDialog(); PackageStore.RemovePackage(u); doc.Close(); } }}总结
以上是内存溢出为你收集整理的c# – 如何从后台线程中使用FixedDocument?全部内容,希望文章能够帮你解决c# – 如何从后台线程中使用FixedDocument?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)