TwinCAT 的多线程消息过滤器

Multi-Thread message filter for TwinCAT

我对 C# 没有经验,所以请原谅我缺乏知识..

我需要为多线程应用程序(后台工作者)实现 COM 消息过滤器。我已发送到此 (https://docs.microsoft.com/en-us/previous-versions/ms809971(v=msdn.10)?redirectedfrom=MSDN) 站点,但它没有提供工作过滤器的示例。 我似乎无法在网上找到多线程消息过滤器。有人有这个过滤器的示例代码吗?

Beckhoff 网站上描述了我遇到的错误(https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_automationinterface/54043195771173899.html&id=,向下滚动直到看到黑人管理员 window,这就是错误),但没有帮助我修复问题。

谢谢!

这是一个 com 消息过滤器的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;
using EnvDTE80;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Collections;

namespace TwincatCodeGenerator
{
    [ComImport(),Guid("00000016-0000-0000-C000-000000000046"),InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter{
        [PreserveSig]
        int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
        [PreserveSig]
        int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
        [PreserveSig]
        int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
    }
    class MessageFilter : IOleMessageFilter
    {
        public static void Register()
        {
            IOleMessageFilter newFilter = new MessageFilter();
            IOleMessageFilter oldFilter = null;
            int test = CoRegisterMessageFilter(newFilter, out oldFilter);
            if (test != 0)
            {
                Console.WriteLine(string.Format("CoRegisterMessageFilterfailed with error : {0}", test));
            }
        }

        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null;
            int test = CoRegisterMessageFilter(null, out oldFilter);
        }

        int IOleMessageFilter.HandleInComingCall(int dwCallType, System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr lpInterfaceInfo)
        {
            //returns the flag SERVERCALL_ISHANDLED.
            Console.WriteLine("HandleIncomingCall");
            return 0;
        }

        int IOleMessageFilter.RetryRejectedCall(System.IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
        {
            //Console.WriteLine("RetryRejectedCall");
            // Thread call was refused, try again.
            if (dwRejectType == 2)
            // flag = SERVERCALL_RETRYLATER.
            {
                // retry thread call at once, if return value >=0 &
                // <100.
                return 99;
            }
            return -1;
        }

        int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
        {//return flag PENDINGMSG_WAITDEFPROCESS.
            Console.WriteLine("Message Pending");
            return 2;
        }

        [DllImport("Ole32.dll")]private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
    }
}

像这样在您的主要方法中调用过滤器:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        MessageFilter.Register();

        //Your code here
        //...

        MessageFilter.Revoke();
    }
}