DLL Loading -- Need a thread delay

DougC used Ask the Experts™
I have a thread loading a DLL and it works great except when I cold boot the machine. The DLL I am loading takes time to load the first time and it hangs up my thread. Any ideas on what I can do here? An example would be nice.

I am using LoadLibrary in InitExtAgent function. Can I use a sleep, or what. It needs to be thread safe. Is there a better way to tell when the LoadLibray has actually completed?

hInst=LoadLibrary("C:\test.dll); //this takes a few seconds
if (hInst)
Do stuff

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
LoadLibrary completes when it returns. in that sense it is a blocking call. as far as i know there is nothing you can do. maybe there is a way to load a dll and get a notification of some kind when it has loaded, maybe via a windows message or something... i have not seen this myself

before your thread is going down, you must use freelibrary. When you create the thread you can take the handle and waitforsingleobject on it.
when the thread is dead, you can free the library


I am freeing up the library when I am done, I just did not show it. Can you be more specific with waitforsingleobject.

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

look at c++ help under CreateEvent or CreateMutex
events and mutexes are objects that are used to synchronize
threads or processes.
an event is kinda bool variable, set to true if the event is set and otherwise false
but instead of a bool, the systeme manages it.
you can make your thread wait for an event/mutex by calling
WaitForSingleObject with the handle of the thread you created.
it will wait exactly until the event is set, so is way better than using sleep, cause you don't waist time.
a mutex is something like an event, but instead of representing a bool, its more than a counter. it is "true" when the counter is zero, "false" otherwise.
wat do u mean hang up? does it like does not respond totally or does it not respond only for a while? if it is like hang for a while, then i understand that it is taking some time to load the dll. else i would understand it as that loading the dll the first time will hang the whole thread totally and become unrecoverable.

are u using ur own dll? how big is the dll? what are u doing inside the dllmain? i had not come across a dll that would take a few seconds to load just bcoz of its size. most prob it has something to do with the dll itself. it could be that u are calling functions such as LoadLibrary, WSAStartup, connect, etc. functions that are not safe.

like makerp, LoadLibrary completes when it returns. you don't really have to test for it. however if u would like to use the library functions for multiple threads, i would suggest that you load it in the main thread. in this way, when u create a new thread, the dll will attach itself to the thread, initializing as needed


Thank You Very Much!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial