This forum has been archived. Please start a new discussion on GitHub.

A question on the issue12

In your article "Integrate ICE with GUI" in Issue 12, you mentioned the topic "Error checking". There,an exception from the working thread is propagated to GUI to show an error message. My question is why not, say, in Windows , using SendMessage() in the Catch as your first choice? I am now in a project with the same question and want to now your consideration on it.

Thanks in advance.


  • matthew
    matthew NL, Canada
    The reason I didn't go for such a solution initially is it is more complex, requiring understanding of message queues, threading, and some more advanced memory management with C++. If you look at issue 15 I explore exactly such a solution, and describe implementations of such an approach with Qt, MFC and Java Swing. For most real world applications, I would use an approach which tells the user that an error has occurred immediately.

    BTW, if you are designing applications based on these articles you should be aware that starting with Ice 3.3 AMI calls will never block the calling thread. You can read about that in
  • Many thanks , Matthew. I read the Issue28. To transfer one Smart pointer from working thread to the GUI thread may be the best choice for me. But
    I still can not figure out how to implement this in win32 by SendMessage(or PostMessage). I don't know how to handle the smart pointer in the sender and receiver. I check the demo/mfc code but not found the sample code.
    I tried to find the similar approach from internet ,say, for boost's shared_ptr, but no explicit result. Could you pls give me some kind of indication?

    Thanks again.
  • matthew
    matthew NL, Canada
    If you look at MFC/server/LogI.cpp you'll find:
    LogI::post(const string& data)
        assert(_hwnd != 0);
        char* text = new char[data.size()+1];
        strcpy(text, data.c_str());
        ::PostMessage(_hwnd, WM_USER, (WPARAM)FALSE, (LPARAM)text);

    When this message is received the text is added to the text control, and then the memory freed.
    CHelloServerDlg::OnLog(WPARAM wParam, LPARAM lParam)
        char* text = (char*)lParam;
        _edit->SetSel(-1, -1);
        delete[] text;
        return 0;

    You can use a similar approach in your application. In this case, a reference counted type is not necessary since the data is received by only a single recipient. If you have multiple recipients, or possibly no recipient then you'd need to take a different approach.
  • Hi, Matthew,
    Yes, You are right. What I am concerning now is the situation as you mentioned:multiple recipients, or possibly no recipient .
    In Issue15, you demonstrated one example with ExceptionHolder in Qt.
    Is it similar to do this in MFC with sendmessage? In other words, the usage of emit error in Qt is similar to sendmessage in MFC? Sorry for my lack of Qt experience. I know in MFC sendmessage needs one pointer to the data ,but can't make sure how to handle one smart pointer (with reference count )here.
    I am sorry this question may beyond the scope of ICE , but any response will be appreciated.

  • matthew
    matthew NL, Canada
    I don't really know enough about the structure of your application to give sensible advise, however, one approach that comes to mind is to put the unit of work in some shared, or global object and use PostMessage to trigger the recipient(s) to consume the work. In this way you are not passing data at all through the PostMessage parameters, but are merely using it as a way to signal the recipient that data is available.