WPF自定义BalloonTips多线程问题 [英] WPF custom BalloonTips problem with multithreading

查看:127
本文介绍了WPF自定义BalloonTips多线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了其他相关问题,但是我无法真正让他们与之相关,所以我认为最好问一下,我对WPF还是很陌生,以此类推,所以请多多包涵。

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.

我正在使用此 http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx api与自定义WPF Windows(在特别是FancyBalloon)。

I am using this http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx api to work with custom WPF Windows (in particular FancyBalloon).

但是,我遇到以下问题,我似乎无法在单独的线程中启动BalloonTips(我需要这样做是因为我正在解析电子邮件,因此如果例如,有3封电子邮件,它显示第一封电子邮件(可以正常工作),但是当涉及第二封电子邮件时,它因TargetInvocationException而崩溃,{指定的元素已经是另一个元素的逻辑子代。请先断开连接。 }。

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."}.

事情是这样的,据称我是在同一个实例上工作,而我曾尝试先调用它以将其关闭,再进行处理等,但无济于事。(然后再次我将其处置,我无法创建另一个实例,因为显然必须从静态线程中调用WPF UI组件,因此在电子邮件循环和显示气球过程中,我试图使用相同的BalloonTip。

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 :/

我想知道是否有是谁

推荐答案

通常,应仅在主UI线程上访问和更新WPF控件。例如,如果您从不是WPF UI线程的任何线程中为数据绑定属性分配数据,则所有数据绑定内部都会进行线程亲和力检查,从而引发异常。

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.

您可以从后台工作线程中删除UI代码,也可以明智地使用SynchronizationContext从后台线程中调用要在UI线程上执行的代码片段。

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.

如果您是WPF的新手或线程技术的新手,则应将UI内容置于后台线程之外。 WPF具有很多复杂性,可以让您忙碌而又不会增加线程问题的麻烦。

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.

这篇关于WPF自定义BalloonTips多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆