Click to See Complete Forum and Search --> : Handling free-threaded callbacks in VB


Simon Gulliver
September 10th, 2001, 10:12 AM
I have a free-threaded component, written in C++, which waits on RS232 I/O and does a callback when reading or writing data.

In VB, I have created a Class Module with "Implements xxxLib.Icallback". This callback works successfully when working with local or global data; so, for example, you can store received data in an array. BUT VB crashes when trying to write to a CONTROL on a form.

In the Win32 API, you would be using PostMessage to send the data across thread boundaries when your callback was received. In VB, I assume that VB is treating the controls as ActiveX objects and it doesn't like talking to an apartment-threaded ActiveX object while in a callback from a free-threaded component. But this doesn't solve my problem...

At the moment, I have the truly dreadful "solution" of saving callback data in an array and polling with a timer in my main form. There must be a better way, surely....

makai
September 10th, 2001, 10:34 AM
I am failing to see how

"BUT VB crashes when trying to write to a CONTROL on a form."

affects the call back from the server object.


It sounds like you are attempting to communicate with third party software rather than handling the callback in your own code (and then passing this information to the control from your form class or other base program over which you have source code control)

cksiow
September 10th, 2001, 08:05 PM
As you expected, VB doesn't like free thread very well. your problem will be solved if you can setup the free-thread component to become apartment thread and marshal the object from the apartment thread to your free thread.... but overhead will be quite large for callback..

you might want to try this, compile this program in VB5, I bet it will run.....

In VB 6, by calling other thread component in the free thread will cause a system error in vb6 runtime.

I have create a class to capture network packet. I create extra thread for backgroud packet capturing.. then when there is a packet, I need to call one of the method of main class to notify the client... so, you can see here, that I do create a free thread and, yes call the method of the component in apartment thread.. but the catch is, you must setup the free thread to become apartment thread & marshal the component.






HTH

cksiow
http://vblib.virtualave.net - share our codes