Re: Monitor Thread In DLL

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: JB (noone_at_nowhere.dom)
Date: 12/10/04

  • Next message: James Brown: "Re: Monitor Thread In DLL"
    Date: Fri, 10 Dec 2004 08:43:59 +0000
    
    

    How about DLL_THREAD_DETACH ? Does this get called sufficiently early?
    If so, you could implement your own reference counting and shut down the
    thread as the last "real" thread exits?

    Matt wrote:
    > Unfortunately, DLL_PROCESS_DETACH is already too late. I see ExitProcess()
    > kill the worker threads using TerminateThread() before DLL_PROCESS_DETACH is
    > called.
    >
    > main() exits in the EXE.
    > Runtime calls atexit() for the EXE.
    > Runtime calls ExitProcess() -> Kills all active worker threads using
    > TerminateThread().
    > Runtime calls DLL_PROCESS_DETACH.
    > Runtime calls atexit() for the DLL during the detach phase.
    >
    > Matt
    >
    >
    > "James Brown" <remove_james_dot_brown7_at_virgin_dot_net> wrote in message
    > news:41b8c2ba$0$3135$cc9e4d1f@news.dial.pipex.com...
    >
    >>Can you not take advantage of the DLL_PROCESS_DETACH
    >>message that your DLL will receive just before it is unloaded??
    >>then signal an event that your monitor thread is listening on,
    >>finally wait for the thread to exit using WaitForSingleObject..
    >>then exit DllMain..
    >>
    >>--
    >>James
    >>---
    >>www.catch22.net
    >>Free win32 software, sourcecode and tutorials
    >>-----
    >>Please de-spam my email address before replying.
    >>"Matt" <none@none.com> wrote in message
    >>news:OJDLoFj3EHA.3624@TK2MSFTNGP14.phx.gbl...
    >>
    >>>Hi,
    >>>
    >>>I am creating a monitor thread in a WIN32 DLL. I'd like the monitor
    >>>thread to stay around for the life of the DLL and then exit cleanly
    >>>before the DLL is detached.
    >>>
    >>>My problem is, I do not know how to "safely" do this. I have tried using
    >>>the atexit() function inside the DLL to trigger the monitor thread
    >>>termination, but it is already too late by then. It looks like the C++
    >>>run time calls:
    >>>
    >>>atexit() for the EXE
    >>>ExitProcess() -> Kills all active worker threads using TerminateThread().
    >>>atexit() for each DLL during the detach phase.
    >>>
    >>>I really want to avoid having my worker thread killed by
    >>>TerminateThread(). I could get around this by requiring the user to call
    >>>some type of cleanup function, but I would like to avoid doing that as
    >>>well (because some users may forget to call it).
    >>>
    >>>CleanupStuff(), waits for thread shutdown. Cleans up the thread nicely.
    >>>atexit() for the EXE
    >>>ExitProcess() -> Kills all active worker threads using TerminateThread().
    >>>atexit() for each DLL during the detach phase.
    >>>
    >>>I could try implicitly loading the atexit() function for the executable
    >>>and add in my cleanup code there, but that seems very much like a hack to
    >>>me.
    >>>
    >>>LoadLib for the atexit() func for the EXE. Call it.
    >>>atexit() for the EXE. Calls my cleanup function and waits for thread
    >>>shutdown. Cleans up the thread nicely.
    >>>ExitProcess() -> Kills all active worker threads using TerminateThread().
    >>>atexit() for each DLL during the detach phase.
    >>>
    >>>Any suggestions would be appreciated.
    >>>
    >>>Thanks,
    >>>
    >>>Matt
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>
    >>
    >>
    >
    >


  • Next message: James Brown: "Re: Monitor Thread In DLL"

    Relevant Pages

    • Re: Monitor Thread In DLL
      ... I would go with a cleanup function. ... if you want to be able to unload the DLL ... > atexit() for the EXE ...
      (microsoft.public.win32.programmer.kernel)
    • Re: Monitor Thread In DLL
      ... Runtime calls atexitfor the EXE. ... Runtime calls atexit() for the DLL during the detach phase. ... > finally wait for the thread to exit using WaitForSingleObject.. ...
      (microsoft.public.win32.programmer.kernel)
    • Re: IMediaControl::Stop() is blocking application during exit.
      ... >>-the object is actually within a DLL for a host application. ... >>Is it illegal to call Stopduring a program exits? ... > function in the DLL before it starts using it, and an uninitialize ...
      (microsoft.public.win32.programmer.directx.video)
    • Re: check if a API exists in dll.
      ... > Be careful with those exits, ... I thought about the first one, if handle is zero there is no DLL, thus ... Insert a FreeLibrary just before the second Exit, properly idented, please ...
      (borland.public.delphi.nativeapi)
    • Re: IMediaControl::Stop() is blocking application during exit.
      ... >>-the object is actually within a DLL for a host application. ... >>Is it illegal to call Stopduring a program exits? ... > IMediaControl::Stopis potentially shutting down a bunch of threads, ... > function in the DLL before it starts using it, and an uninitialize ...
      (microsoft.public.win32.programmer.directx.video)