Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Imagine I have some networking class CNetwork which has some method called: makeNetworkRequest(int requestCode). The response from makeNetworkRequest(int requestCode) can of course come some time later this function was called. What I want is that, when that response arrives, to execute such code for example:

C++
void myApp::function1()
{
CMyButton button;
button.SetText(responseFromServer.text);
button.SetTextColor(responseFromServer.color);
}


The thing I want now is that the user from UI may be calling makeNetworkRequest with different request codes -- and based on that I may want to execute different functionality, e.g., for example execute function2 instead of function1 -- and I want to be able to achieve the functionality I mentioned above.
C++
void myApp::function2()
{
CMyEdit edit;
edit.SetText(responseFromServer.text);
}
Posted

There are really many ways to achieve what you intend to do. For instance:
  • A switch statement with function calls hard-coded for every case
  • A classical, C-like callback mechanism, like the one used in Windows API (see EnumWindowsProc[^]). With this approach, you have to use static methods, though
  • Using member function pointers
  • Using polymorphism, that is pass an object implementing the interface containing the method 'function'. Different objects implement such method in different ways.
  • ...
 
Share this answer
 
Comments
[no name] 15-Feb-13 5:03am    
>>"Using polymorphism, that is pass an object implementing the interface containing the method 'function'. Different objects implement such method in different ways."

Could you please elaborate on this approach??
Sergey Alexandrovich Kryukov 12-Mar-13 17:14pm    
Sure, a 5.
—SA
You would probably like to use I/O Completion Ports[^]

Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports[^]

You can use BindIoCompletionCallback[^], and I guess you'll find this article of some interest:IOCPNet - Ultimate IOCP[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Mar-13 17:13pm    
Sure, a 5.
—SA
Espen Harlinn 12-Mar-13 17:29pm    
Thank you, Sergey :-D
give a look to std::function[^] and related header.
 
Share this answer
 
v2
In addition to the ones already mentioned, it's worth considering using the MFC CWnd message pump system, as it looks like you're using MFC.

This especially important as you really shouldn't manipulate GUI elements from another thread.

In this case, you could define a separate message for each request code, or pass the request code on as a message parameter and let the message handling function decide what function to call.

See Meandering Through the Maze of MFC Message and Command Routing[^] for a very good explanation of the MFC messaging system, or just look at How to send a user-defined message with SendMessage, PostMessage or PostThreadMessage[^]
 
Share this answer
 
Comments
[no name] 14-Feb-13 8:51am    
Hi, no I am not using MFC this was just an example. I might use Blackberry 10 SDK. Indeed manipulating UI is my concern now.. I might need to somehow embed the UI code (e.g., [1]) "somewhere" so that it gets called once the server returns... desirably this UI code should also then be able to use the response data from the server to update it's values etc...

[1]: CSomeUIElement m = new CSomeUIElement(serverresponse.data)... etc. etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900