Because of Dragokas the thread, I checked my hard disk and found asynchronous code downloads about wininet. I feel that there is too little information here and I hope someone can improve. At the same time the search forum found this thread, the lesson can help
I don't have an answer, but just a brief check of that other thread, shows that at the time Lavolpe didn't quite understand that any other callback thread initiated by another method/api call (in this case wininet) needs the runtime initiated on the thread before it can use any runtime functions, or error checking, or even declared APIs (not in a typelib).
You can't even use (shared) variables or data outside the local (stack based) ones within the function, unless you use synchronization or marshalling/serializing or IPC.
Things to keep in mind if you are servicing a callback on a thread not created by yourself.
The first order in servicing a callback from on another thread should be getting the VB6 runtime initiated.
Either that or you can keep the actual callback functionality very minimal, and use typelib APIs to either post messages back to the main thread (this is what i do) or use synchronization to access global variables.
Last edited by DEXWERX; Jun 12th, 2018 at 07:19 AM.
The C thread has access to some VB variable addresses which it writes to directly (no sync) and seems to work.
I guess technically that wouldn't cause an automatic crash... assuming you only read the data from VB, and write from C.
You just have no guarantee when the data is updated. But you can try coordinate with a PostMessage back to the main thread.
Trying to figure out, how to make that work is making my head hurt... I think when it comes to threading, I'll just go by the book and use proper sync.
Last edited by DEXWERX; Jun 12th, 2018 at 03:46 PM.
as far as VB6 on x86 is concerned, Integer writes are atomic. I would just never do it
As for subclassing, I made a crashproof subclasing dll, so it's as convenient as checking off a reference.
I get people don't like 3rd party dependencies (myself included),
But since know what's in it, I'm happy to use it myself.
Last edited by DEXWERX; Jun 12th, 2018 at 05:23 PM.
You can use this module to receive a callback function from an arbitrary thread. It allows to initialize the runtime and project-specific data. You can debug it in the IDE as well.
just to test the direct memory access code a little deeper I modified the c example dll i gave earlier so that
it now can handle multiple async background downloads each in their own thread.
The var writes are to private class variables and the class has a check on terminate to
auto abort and wait until the downloader thread exits to prevent any crashs.
the main form occasionally polls the class for current download status on a timer
still seems ok in testing