WPF自定义BalloonTips多线程问题
我读过其他相关问题,但我无法真正让它们与此相关,所以我认为最好问一下,我对 WPF 等还很陌生,所以请耐心等待。
我正在使用此 http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx
api 来处理自定义 WPF Windows(特别是 FancyBalloon)。
但是,我遇到了以下问题,我似乎无法在单独的线程中启动 BalloonTips(我需要这个,因为我正在解析电子邮件,因此如果有 3 封电子邮件,它会显示第一封电子邮件(可以使用)很好),但是当涉及到第二封电子邮件时,它会崩溃并出现 TargetInitationException ,{“指定的元素已经是另一个元素的逻辑子元素。首先断开它。”}
事情是,我应该使用同一个实例,并且我有尝试调用它来关闭它之前,处置它等但无济于事(然后,如果我处置它,我无法创建另一个实例,因为显然 WPF UI 组件必须从静态线程调用,因此在整个电子邮件循环+显示气球中。 ,我正在尝试使用相同的 BalloonTip,
请问有什么建议吗?我真的很困惑,而且我已经使用它很长一段时间了:/
我想知道是否有人。
I have read other related question but i cant really get them to relate to this so I thought it were best to ask, Im pretty new to WPF and so on so please bear with me.
I am using this http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx
api to work with custom WPF Windows (in particular FancyBalloon).
However, i'm coming across the following problem, I seem unable to start off BalloonTips in a separate thread ( i need this because i'm parsing emails and hence if there are 3 emails for instance, it displays the first email (that works fine), but when it comes to the second email it crashes with a TargetInvocationException , {"Specified element is already the logical child of another element. Disconnect it first."}.
Thing is, im supposedly working with the same instance and i have attempted calling it to close it before, disposing it etc but to no avail. (then again if i dispose it, i cant create another instance as apparently WPF UI components must be called from a static thread so throughout the looping of emails + displaying balloon, i am trying to use the same BalloonTip.
Any suggestions please? I am really at a loss here and i've been on it for quite a while now :/
I was wondering if there was anyone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,WPF 控件只能在主 UI 线程上访问和更新。例如,在整个数据绑定内部都有线程关联性检查,如果您从非 WPF UI 线程的任何线程分配数据绑定属性,则会引发异常。
您可以从后台工作线程中删除 UI 代码,或者明智地使用 SynchronizationContext 从后台线程调用代码片段以在 UI 线程上执行。
如果您是 WPF 新手或线程新手,则应该将 UI 内容保留在后台线程之外。 WPF 具有足够的复杂性,可以让您忙碌起来,而不会增加线程问题的额外麻烦。
In general, WPF controls should be accessed and updated only on the main UI thread. There are thread affinity checks all over the data binding innards that will throw an exception if you assign to a data bound property from any thread that isn't the WPF UI thread, for example.
You can either remove the UI code from your background worker thread, or make judicious use of a SynchronizationContext to invoke from your background thread snippets of code to execute on the UI thread.
If you're new to WPF or new to threading, you should keep the UI stuff out of the background threads. WPF has plenty of complexity to keep you busy without adding the additional headache of threading issues to the mix.
您似乎同时将相同的 UI 元素添加到多个父容器中。
例如,如果我在单独的线程中同时尝试 myStackPanel.Children.Add(myUIElement),引用同一个 UI 元素对象,这将导致您看到的错误。
如果多个线程需要相同的 UI 元素,请考虑克隆它们或将 UI 逻辑移至主线程。
It seems you are adding the same UI element to multiple parent containers at the same time.
For example, if I attempt myStackPanel.Children.Add(myUIElement) concurrently in separate threads, referring to the same UI element object, this would cause the error you are seeing.
If you need the same UI elements for multiple threads, consider cloning them or moving your UI logic to the main thread.