Outlook 电子邮件发送事件在后台线程上引发

发布于 2024-09-17 17:46:21 字数 930 浏览 12 评论 0原文

我正在创建 Outlook 邮件项目对象并监视发送事件。一切看起来都很好,但是当发送事件触发时,该事件是在我的应用程序的后台线程上引发的。这是一个问题的原因是,此事件创建了通过我的应用程序发送的电子邮件的记录,该记录被添加到对象集合中。因此,集合在后台线程上引发 List Changed 事件,从而导致对显示集合的控件进行跨线程控件访问。

这是我的事件连接:

((Outlook.ItemEvents_10_Event)item).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(ItemSendWatcher_Send);

该事件连接在一个基本类中,该类保存对正在发送的消息的引用,以及引发发送事件时调用的委托。这允许我将电子邮件对象的实例传递给委托。

这是我的处理程序:

void ItemSendWatcher_Send(ref bool Cancel)
    {
        if (itemSendDelegate != null)
        {
            this.itemSendDelegate(this.item, ref Cancel); //The delegate with the mail item
        }
        Marshal.ReleaseComObject(item);
        itemSendDelegate = null;
    }

这是该事件的正确行为还是我在构造对象时做错了什么?感谢您的任何帮助。

编辑:澄清一下,我不是在 UI 层中处理事件,而是在业务对象中处理事件。从发送事件处理程序调用的委托会导致在内部将新对象添加到列表中,从而引发列表的 ListChanged 事件,从而导致在显示此列表的控件中调用处理程序。我希望这能澄清我想要实现的目标。

I am creating an Outlook Mail Item object and watching for the Send event. All appears well, but when the Send event fires, the event is raised on the background thread of my application. The reason this is a problem is that this event creates a record of the email sent through my application, which is added to a collection of objects. The collection is therefore raising the List Changed event on the background thread causing a cross-thread control access on the control that displays the collection.

Here is my event hookup:

((Outlook.ItemEvents_10_Event)item).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(ItemSendWatcher_Send);

The event is hooked up in a basic class which holds a reference to the message being sent, and a delegate to call when the send event is raised. This allows me to pass an instance of the email object to the delegate.

This is my handler:

void ItemSendWatcher_Send(ref bool Cancel)
    {
        if (itemSendDelegate != null)
        {
            this.itemSendDelegate(this.item, ref Cancel); //The delegate with the mail item
        }
        Marshal.ReleaseComObject(item);
        itemSendDelegate = null;
    }

Is this the correct behaviour for this event or am I doing something wrong when constructing the object? Thanks for any help.

Edit: Just to clarify, I am not handling the event in the UI layer, rather in a business object. The delegate called from the Send Event handler causes a new object to be added to a list internally, which causes the list's ListChanged event to be raised resulting in a handler being called in the control displaying this list. I hope this clarifies what I'm trying to achieve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-09-24 17:46:21

许多表单事件都是在主窗口线程上引发的,这意味着您通常不必太担心跨线程问题。然而,没有任何保证,COM 通常不会在该线程上引发事件。解决方案是使用方法委托或匿名块来调用 this.Invoke(..),以在正确的线程上执行必要的工作。要测试是否有必要测试 this.InvokeRequired。

A lot of Forms events are raised on the main window thread which means you often don't have to worry too much about cross thread concerns. However there are no guarantees and COM will usually not raise events on that thread. The solution is to call this.Invoke(..) with either a delegate to a method or an anonymous block to perform the necessary work on the right thread. To test whether or not this is necessary test this.InvokeRequired.

百合的盛世恋 2024-09-24 17:46:21

您需要在 ItemEvents_10_SendEventHandler() 内处理 InvokeRequired + Invoke

You need to handle InvokeRequired + Invoke inside ItemEvents_10_SendEventHandler()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文