A simple Modeless AfxMessageBox

10 12 2008

There were times when AfxMessageBox freezes my background process thread (socket communication, etc.) Here’s what I wrote as a quick and dirty solution. Put it in your StdAfx.h

You can refine it if you like.

//////////////////////////////////////////////////////////////////////////
// ivo setyadi [12/10/2008 Development]
// a simple (or semi) modeless AfxMessageBox using MFC thread
// usage: 
// CMy my; 
// my.ModelessBox("your message");
class CMy
{
public:	
	void ModelessBox( CString str) 
	{
		char *buffer=new char[256]; 
		lstrcpy(buffer,str);		 				
 		AfxBeginThread(showMessage, (LPVOID *) buffer,   THREAD_PRIORITY_NORMAL) ;
	}	
	
	static UINT showMessage(LPVOID lParam)  
	{  
		if(lParam == NULL)
			AfxEndThread(NULL);
		
		char *pStr = (char *) lParam;
		AfxMessageBox(pStr);  		
		delete pStr; // don't forget
		
		return TRUE;
	}  
};

There.