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.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: