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 6736593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:52+00:00 2026-05-26T11:08:52+00:00

I think I am right in thinking that if I have a program like

  • 0

I think I am right in thinking that if I have a program like this which sets up a callback function.

void MyCallbackFunction(char* data) {
cout << "some data arrived: " << data << endl;
}

int main(){
   //sets up callback
   SetDispatchFunction(&MyCallbackFunction));

   while(1==1) {
  sleep(1000);
   }

   return 0;
}

then because this is a single threaded program and execution will be always handling the while loop, there is no way for the program to handle the MyCallbackFunction handler?

If so, assuming I don’t want to use multiple threads, what options do I have to allow processing of the callback function?

I decided to include a real example to demonstrate.

#include <iostream>

using namespace std;

#define WIN32_LEAN_AND_MEAN
#include <windows.h>


BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam)
{
   char szText[100] = {0};
   GetWindowText(hwndChild, szText, 100);
   if (lstrlen(szText) < 1) return true;

   cout << "Window text: " << szText << endl;

   return TRUE;
}

int main(int argc, char* argv[])
{
   //latch onto Google Chrome if running
   HWND windowHandle = FindWindow("Chrome_WidgetWin_0", 0);

   if(windowHandle)
      EnumChildWindows(windowHandle, EnumChildProc, 0);


   //loop so program doesn't stop
   while(1==1) {
      Sleep(2000);
   }

   return 0;
}
  • 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-26T11:08:53+00:00Added an answer on May 26, 2026 at 11:08 am

    The callback function usually gets called in the thread spawned by the entity you register with, You will need some sort of an signalling mechanism, by which you make your main thread aware of the callback. It is always advisable to not perform your processing in the thread, instead one should switch the context and do the processing in one of their own threads.

    Just consider as an Example:
    Main thread blocks on a message Queue(or periodically checks for the same), waiting to read a message and in the callback function you post a message to this message queue.

    You could also use an global protected by synchronization to achieve a similar functionality without message Queue.

    However, If your program is strictly single threaded,then at some point of time the while loop will be interrupted and the callback function will get executed,wherein you can setup a global variable, which then can be checked by the while when it gets interrupted.Ofcourse,Synchronization will be needed.

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

Sidebar

Related Questions

I think this must be simple but I can't get it right... I have
I'm trying to start out with LinqtoXml. I have added (I think) the right
I think I might have done something right, headByRating and headByName both refer to
I think most people know how to do this via the GUI (right click
I have an appliction right now that is a pipeline design. In one the
I have a question which is mostly answered by this question . However, I
I have a psychological tic which makes me reluctant to use large libraries (like
I am working on a project that i want to have a plugin-sandbox like
I have a file that contains this: (Float,Float,Float)sometext (Float,Float,Float)sometext (Float,Float,Float)sometext ... ... and I
I think im asking for the right type of report from Quickreport. What we

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.