Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:31:41+00:00 2026-05-24T16:31:41+00:00

I have a native Windows DLL written in C. This DLL is designed to

  • 0

I have a native Windows DLL written in C. This DLL is designed to be loaded (injected) in different Windos processes. This DLL creates some working threads that look like this:

while(1) {
    // do work
    Sleep(few secs);
}

The problem is if the process exits and DLL unloads while thread is in Sleep() it will crash the process when it comes back from Sleep (thread will try to execute code that is not there). I was thinking of using TerminateThread() with the DllUnload handler but MSDN says it should be used in most extreme cases only. For example – my threads use critical sections and the documentation says:

If the target thread owns a critical section, the critical section will not be released.

How do I cleanly close the thread that is sleeping when the DLL is unloading? Should I redesign my working threads to do work in some other way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:31:41+00:00Added an answer on May 24, 2026 at 4:31 pm

    Create an event:

    HANDLE threadTerminationEvent = CreateEvent(.....);
    

    Use WaitForSingleObject instead of Sleep:

    while (1)
    {
        ...........
        if (WaitForSingleObject(threadTerminationEvent, few secs) != WAIT_TIMEOUT)
            break;
    }
    // release resources and end thread
    

    Wait for the thread to end itself:

    SetEvent(threadTerminationEvent); // “please, die”
    WaitForSingleObject(hThread, INFINITE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some Windows native applications have a question mark icon on the title bar. It's
I have a Windows service(C#), that sprawns few child native processes (C++). I'd like
I have some native (as in /SUBSYSTEM:NATIVE ) Windows programs that I'd like to
I have many C/C++ old native .exe and .dll programs running on Windows servers
This is the output I am getting and 'abc.exe': Loaded 'C:\Windows\system\glut32.dll', Binary was not
I have a DLL written in C# which is acessed by a native EXE
We have Win Forms application uses the 3rd party windows native controls. Can these
Windows resources have a FileVersion and a ProductVersion. In native development environment(eg. Win32 app,
My weird monitor's native resolution isn't recognized by Windows, so I have to set
I have a native VC++ project that uses a dll (which is not in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.