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

  • Home
  • SEARCH
  • 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 677519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:02:54+00:00 2026-05-14T01:02:54+00:00

I have a simple program that creates a thread, loops twenty times and then

  • 0

I have a simple program that creates a thread, loops twenty times and then makes a call to close itself and perform the necessary cleanup.

When I debug the program it reaches the ExitThread(); method and pauses, ignoring the printf(); I have set up after it to signal to me it’s closed.

Is this normal or am I forgetting to do something? I’m new to threading using C.

Main()

void main()
{
    Time t;
    int i = 0;

    StartTimer();

    for(i = 0; i < 20; i++)
    {
        t = GetTime();
        printf("%d.%.3d\n", t.seconds, t.milliseconds);
        Sleep(100);
    }
    StopTimer();
}

Thread Creation

void StartTimer()
{
    DWORD threadId;
seconds = 0;
milliseconds = 0;

// Create child thread
hThread = CreateThread(
    NULL,       // lpThreadAttributes (default)
    0,          // dwStackSize (default)
    ThreadFunc, // lpStartAddress
    NULL,       // lpParameter
    0,          // dwCreationFlags
    &threadId   // lpThreadId (returned by function)
    );

// Check child thread was created successfully
if(hThread == NULL)
{
    printf("Error creating thread\n");
}
}

Thread Close

void StopTimer()
{
    DWORD exitCode;

    if(GetExitCodeThread(hThread,&exitCode) != 0)
    {
        ExitThread(exitCode);   
        printf("Thread closed");

        if(CloseHandle(hThread))
        {
            printf("Handle closed");
        }
    }
}

Edit:

I’ve realised that ExitThread() stops the executing thread, therefore I have attempted to use a different method to stop the timer but i’m still left with the same result.

void StopTimer()
{    
    WaitForSingleObject(hThread,INFINITE);
}

Solution:

The reason the program was stopping at the WaitForSingleObject() call and not progressing no matter how long it took was because the operation it was waiting on had an infinite loop.

It would be waiting for an infinite loop to finish, not the best thing to do. Changing the delay of WaitForSingleObject() to something more reasonable, such as 100 allowed the program to run normally.

Thanks for all the input.

  • 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-14T01:02:54+00:00Added an answer on May 14, 2026 at 1:02 am

    Yes this is “normal”, by calling ExitThread, you’re telling to windows you’re done with that calling thread, so the rest of the code isn’t going to get called. It’s a bit like calling exit(0).

    Personally I wouldn’t use it like this. The rest of the code is OK, you’re properly cleaning up the thread, just exit your thread function normally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 363k
  • Answers 363k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a private variable with a setter. In the setter… May 14, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer Simply an IE bug. I don't think there are active… May 14, 2026 at 3:17 pm
  • Editorial Team
    Editorial Team added an answer Create a new instance of DBContext the second time. But… May 14, 2026 at 3:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.