WPF从其他线程访问GUI
我正在满足仅创建 WPF 应用程序单个实例的要求。 但是 - 我必须将命令行传递到第一个实例,然后执行一些 UI 操作。
我使用 Mutext 来检查已经运行的实例,我使用 NamedPipes 将命令行传输到已经运行的实例。
但当然我不在正确的线程中访问“Window1”。 我尝试在静态类中存储对“Window1”的引用,然后使用调度程序调用“Window1”中的方法,但是,一旦我尝试访问变量(“Window1”中的类范围),我就会收到“未将对象引用设置为对象的实例。”
UI操作是向TabControl添加一个新选项卡 - 在新选项卡的初始化期间完成了一些工作 - 并且变量被初始化,甚至我想调用的方法在初始化期间工作 - 但是当从调度程序调用时它失败。
任何提示,如何做到这一点?我在这里走错路了吗?
谢谢!
I am working through the requirement to make a WPF Application single instance only.
However - I have to pass the command line to the first instance and then perform some UI action.
I am using a Mutext to check for already running instances, I do use NamedPipes to transfer the command line to the already running instance.
But of course I am not in the correct Thread to access "Window1".
I tried to store a reference to "Window1" in a static class and then use the Dispatcher to call a Method in "Window1", however, as soon as I try to access a variable (class wide scope in "Window1") I receive a "Object reference not set to an instance of an object."
The UI Action is to add a new Tab to a TabControl - during initialization of the new Tab some work is done - and the variables are initialized and even the method I want to call works during the init - but when called from the Dispatcher it fails.
Any hints, how to do this? Am I on the wrong track here?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很简单:
您可以从 App.Startup 以及从命名管道接收消息的线程中调用它。
这里的关键考虑因素是:
This is easy:
You can call this from your App.Startup and also from your thread that receives messages from the named pipe.
The key considerations here are:
这是不对的,您确定互斥体将控制正确传递给当前正在运行的应用程序实例吗?
如果是线程 UI 访问问题,您应该收到此错误:调用线程无法访问此对象,因为不同的线程拥有它。
事实上,您收到“对象引用未设置到对象实例”。错误消息意味着您尚未将该对象实例化为新对象。
That's not right, are you certain that the mutex is passing control correctly to your currently running instance of the application?
If it was a thread UI access issue, you should have received this error: The calling thread cannot access this object because a different thread owns it.
The fact that you're getting an "Object reference not set to an instance of an object." error message means that you've not yet instantiated the object as new.