Handling multi-threaded .NET events

Posted by a.j.piper on 02-Jan-2018 04:47

Hi,

I'm trying to integrate to a 3rd party .NET assembly (Creditcall ChipDNA SDK - Pre-Certified - https://www.creditcall.com/developers) and I'm hitting the dreaded "You are trying to use a multi-threaded .NET object in a way that is not supported.  The ABL cannot be called on a thread other than the main thread. (15740) " error.

From what I can gather, I need to write a wrapper assembly in C#/VB.NET to raise the events to the main thread. However I'm struggling to find any example code where people have done this (I'm not too familiar with either language).

Has anybody ever successfully integrated an assembly that uses multi-threaded events?

Thanks

Andrew

Posted by marian.edu on 02-Jan-2018 05:14

What did worked for me was to save the current dispatcher in the wrapper constructor and then use that with invoke to either call back on OE handler that you get injected in the constructor or you just publish an event to which you subscribe to from OE side…


public event EventHandler MyEvent;

MyWrapper (IOEHandler handler) {
dispatcher = Dispatcher.CurrentDispatcher;
oeHandler = handler;
}

OnSomeEventPublish (object sender, EventArgs evt) {
EventHandler handler = MyEvent;

        if (handler != null)
dispatcher.Invoke(handler, new object[] { this, evt }); 
}


Marian Edu

Acorn IT 
+40 740 036 212

All Replies

Posted by marian.edu on 02-Jan-2018 05:14

What did worked for me was to save the current dispatcher in the wrapper constructor and then use that with invoke to either call back on OE handler that you get injected in the constructor or you just publish an event to which you subscribe to from OE side…


public event EventHandler MyEvent;

MyWrapper (IOEHandler handler) {
dispatcher = Dispatcher.CurrentDispatcher;
oeHandler = handler;
}

OnSomeEventPublish (object sender, EventArgs evt) {
EventHandler handler = MyEvent;

        if (handler != null)
dispatcher.Invoke(handler, new object[] { this, evt }); 
}


Marian Edu

Acorn IT 
+40 740 036 212

Posted by a.j.piper on 02-Jan-2018 06:26

Thanks Marian,

That's saved my bacon! I was missing a reference to WindowsBase.dll in my C# assembly project, and thus was trying various Invoke and BeingInvoke calls, only to be told they didn't exist!

Andrew

This thread is closed