0

I need to notify a user about some information using message box in a notification app, but it shouldn't block the program (input and output in console).

I was thinking about using separate thread for every notification, but it seems to take too much resources. Maybe in this case it isn't so?

My example code is like this:

#include <iostream>
#include <string>
#include <windows.h> 

void showMessage(std::string message)
{
    std::wstring widestr = std::wstring(message.begin(), message.end());
    const wchar_t* widecstr = widestr.c_str();

    MessageBoxW(NULL, (LPCWSTR)(widecstr), (LPCWSTR)L"Notification", NULL);
    
}

int main()
{
    while(1)
    {
        std::string str;
        std::cin >> str;
        if(str == "0")
            break;
        showMessage(str);
    }
    return 0;
}

What should I add to it? Thanks!

Jr_vv
  • 41
  • 4

1 Answers1

-1

I suppose you can call MessageBox from a different thread, in that case it won't block your main thread. Please take a look at the following question: How do you create a Message Box thread? there is also a code example.

If you don't want to block the main thread, there is no other way, rather than a separate thread. Of course, it depends on the number of threads you create, so I'd suggest you to count current number of active windows and force user to close them if he wants to go on.

Arman Oganesyan
  • 396
  • 3
  • 11