Raising COM events from a thread

101 views
Skip to first unread message

Lukas Kavicky

unread,
Aug 7, 2018, 9:42:55 AM8/7/18
to Excel-DNA
Hi!
I'm trying to enable event-based communication between a VBA class and class of my COM-library project created using ExcelDNA and written in VB.Net. The very basic experiment is working fine and raises the event in VBA:

VB.Net:
Imports System.Runtime.InteropServices

<ComVisible(True)>
<ComClass>
Public Class ComClassExp1
    Public Event NewMessage(msg As String)

    Public Sub SendMessage(str As String)
        GetMessage_ThreadJob()

    End Sub

    Private Sub GetMessage_ThreadJob()
        MsgBox("Thread - Sending message")
        RaiseEvent NewMessage("Message")
    End Sub

End Class

However, I would need the GetMessage_ThreadJob to run in a separate thread, and in such case the event is either not raised or the whole Excel application crashes.
This is my code:


<ComVisible(True)>
<ComClass>
Public Class ComClassExp1
    Public Event NewMessage(msg As String)

    Public Sub SendMessage(str As String)
        MsgBox("Message is here, running thread")
        Dim iThread As New System.Threading.Thread(AddressOf GetMessage_ThreadJob)

        With iThread
            .IsBackground = True
            .Priority = Threading.ThreadPriority.BelowNormal
            .Start()
        End With

    End Sub

    Private Sub GetMessage_ThreadJob()
        MsgBox("Thread - Sending message")
        RaiseEvent NewMessage("Message")

    End Sub


End Class

What can be done to raise the event from thread correctly?

Lukas

Govert van Drimmelen

unread,
Aug 7, 2018, 10:16:07 AM8/7/18
to exce...@googlegroups.com

You might need to understand what the COM apartment model of VBA is, and how that interacts with the .NET COM interop.

A start would be this: https://support.microsoft.com/en-us/help/150777/info-descriptions-and-workings-of-ole-threading-models

But that doesn’t tell you anything about the .NET / COM interop world – for this I have the 1600 page, two volume reference by Adam Nathan https://support.microsoft.com/en-us/help/150777/info-descriptions-and-workings-of-ole-threading-models which seems to be the only comprehensive reference.

 

Do you have any reason to believe multi-threaded code is safe in VBA under any circumstances?

And why can’t you do the tricky stuff on the .NET side?

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Lukas Kavicky

unread,
Aug 8, 2018, 10:47:22 AM8/8/18
to Excel-DNA
Hi Govert,

thank you for your reply. The examples I've used were probably not really good to demostrate what I want to achieve.
I don't really want the VBA to run in multiple threads. In my COM class, I would like to use an already existing VB.Net class having a GetDataAsync method, which  starts a separate thread to acquire the desired data (the operation takes some time)  and returns the data via the DataArrived event of the class.  And in the handler of the DataArrived event  (still on the .Net side) I would like to raise an event of the COM class so that the data could be handled by the VBA, but there is no need for parallel execution. I've tried to use the ExelAsyncTool, but was not able to make it work.
Is there a way to raise the event on the main thread from the (potentially multiple) threads getting the data?

I have to admit that I'm new to the COM world, so even though I have read the article you've sent, I'm not sure if what I want to achieve is possible or not.

Lukas
Reply all
Reply to author
Forward
0 new messages