0

I'm trying to implement a C# COM server that raises COM events in Excel based on this example: http://msdn.microsoft.com/en-us/library/dd8bf0x3(v=vs.90).aspx

My question is similar to the following

Exposing C# COM server events to Delphi client applications

I'm using Visual Studio 2015, net 4.5 and Excel 2013

Here is the example test code

namespace XLServer
{
    [ComVisible(true)]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
    public interface Events
    {
        void NewEvent();
    }

    [ComVisible(true)]
    [ComSourceInterfaces(typeof(Events))]
    public class MessageHandler
    {
        public delegate void NewEventDelegate();
        public event NewEventDelegate NewEvent;

        public System.Threading.Timer EventTimer;

        public MessageHandler() { StartEvents(); }

       public void FireNewEvent()
        {
            Debug.Print("Fire New Event");
            if (NewEvent != null) NewEvent();
        }

        public void StartEvents()
        {
            if (EventTimer == null) EventTimer = new System.Threading.Timer(MessageEvent, null, 1000, 2000);
        }
        private void MessageEvent(object o)
        {
            FireNewEvent();
        }
    }
}

Here is the VB code

Public WithEvents mh As MessageHandler
Public status As String

Public Sub Class_Initialize()
    Set mh = New MessageHandler
    status = "Initialized"
End Sub

' Events and methods are matched by name and signature.  
Public Sub mh_NewEvent()
    On Error Resume Next
    MsgBox ("NewEvent Fired")
End Sub

I get the following error in the FireNewEvent method when I start the MessageHandler in Excel

Fire New Event
Exception thrown: 'System.Reflection.TargetException' in mscorlib.dll
An unhandled exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll
Additional information: Object does not match target type.

Any suggestion would be appreciated. Thanks!

Update

The problem is due to using the System.Threading.Timer.

It works correctly when using the System.Windows.Forms.Timer

namespace XLServer
{
    [ComVisible(true)]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
    public interface Events
    {
        void NewEvent();
    }

    [ComVisible(true)]
    [ComSourceInterfaces(typeof(Events))]
    public class MessageHandler
    {
        public delegate void NewEventDelegate();
        public event NewEventDelegate NewEvent;

        public System.Windows.Forms.Timer EventTimer;

        public MessageHandler() { StartEvents(); }

       public void FireNewEvent()
        {
            Debug.Print("Fire New Event");
            if (NewEvent != null) NewEvent();
        }

        private void StartEvents()
        {
            if (EventTimer == null) EventTimer = new Timer();
            EventTimer.Interval = 5000;
            EventTimer.Tick += new EventHandler(timer_Tick);
            EventTimer.Start();
         }
        private void timer_Tick(object sender, EventArgs e)
        {
           FireNewEvent();
        }
    }
Community
  • 1
  • 1
ateene
  • 1
  • 1
  • This happens because you raise the event on a worker thread. Crystal ball says that you didn't register your dll correctly. You have to use regasm.exe's /tlb option so the type library info is written to the registry and can be found back at runtime to help marshal the call to the VBA thread. – Hans Passant Feb 06 '17 at 23:02
  • The problem is actually due to using the System.Threading,Timer. It works when I switched to System.Windows.Forms.timer – ateene Feb 07 '17 at 13:26

0 Answers0