|
Hi,
Need more help.
I have a single instance of a CAsyncSocket derived class, which is acting as a listening daemon.
When i accept a new connection, I create a new instance of a new object, which is again a CAsyncSocket derived class. This new object is tuned to handle the SMTP conversation itself.
CMailConnection *p_MailConn = new CMailConnection;
SOCKADDR_IN pSockAddr;
int nSockAddrLen=sizeof(pSockAddr);
if (Accept((*p_MailConn),(sockaddr *) &pSockAddr,&nSockAddrLen) == 0)
Now I want to enforce a limit on the number of concurrent connections. I can see two ways of doing this... One is messy and involves manually keeping a count of connections. The other, better way (IMHO) is to use semaphores. This way I can make my OnAccept thread wait for a free connection using the semaphore on WaitForSingleObject().
However, for this to work, I need to get each instance of my CMailConnection object running in it's own thread. I've used worker threads before, ie. calling a function within AfxBeginThread() to do some heavy processing. But I don't know how to start a thread in this case. Can't see that I can use the regular worker threads model: MSDN says that the thread is killed when the pfnThreadProc returns.
Evidently, there is some internal MFC trickery inside of CAsyncSocket that handles the event functions like OnReceive() etc. Dunno where this code is or if it's relevant.
Help!
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Since you are using a Derived class from CAsyncSocket, you could simply add a static member variable to the derived class.
Then you would expose a static member function of the class that will create a new instance of the class only if the static count is less than the limit that you want to impose. When ever the objects are destroyed, they will decrement the count of current objects.
Then you can hide the constructors by declaring them private, this will make it so that the only way that users of your class can create a new object by calling your static member function.
The thread solution that you were talking about should be able to work, you would need to structure your worker thread procedure to start, create your new object, attempt to create the connection, then loop while it is working. It would only terminate with some message or event that you set for the thread. With out really knowing the details of what this thread needs to perform, I cannot give any more hints for this solution.
|
|
|
|
|
Thanks for those pointers.
Would a "User interface thread" be a help? the UI thread appears to enter is own message loop and runs until explicitly told to stop.
Jon
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|
Hi all
I need some help. I am making an mp3 player and i designed an emblem for the media player but i dont know how to insert the image into MFC. Can someone please lend me a hand and tell me how i can load the image from my harddrive to a "Picture" control in the dialog Box.
Thanks
|
|
|
|
|
Start at CStatic::SetBitmap, you should be able to figure it out from there.
|
|
|
|
|
You can either insert your bitmap into your resource file, associate the bitmap with the picture control at design time and the dialog will load the image for you.
Or you can Load the bitmap with LoadImage (LoadBitmap calls loadImage), then you can use the HBITMAP handle that you receive from LoadImage and send a STM_SETIMAGE message.
<br />
SendMessage( <br />
(HWND) hWnd,
STM_SETIMAGE,
(WPARAM) wParam,
(LPARAM) lParam
);<br />
Parameters
wParam
Specifies the type of image to associate with the static control. This parameter can be one of the following values:
IMAGE_BITMAP
IMAGE_CURSOR
IMAGE_ENHMETAFILE
IMAGE_ICON
Teh MFC equivalent is probably CStatic::SetImage
|
|
|
|
|
I have an MFC app with one CMainFrame, one doc, and multiple views, each view is associated with the same doc but different child frames (CMDIChildFrame).Each view handle certain set of manu commands.
If I have updated the doc's data thru one of the views, how do I broadcast the command so that other views can respond the command?
|
|
|
|
|
You can invoke the UpdateAllViews method of CDocument.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Yes, UpdateAllViews() informs all other views to update their contents. What about if I want other views to respond in a way like following:
if ID_COMMAND1 is send by pView1
then I want:
pView2->WelcomeYou();
pView3->MakeDinnerForYou();
...
pViewn->GoFishing()..
Any idea?
|
|
|
|
|
Other than using the hint parameters, no, I don't have any ideas. Not totally sure what you want.
If X happens in view 1, then you want view 2 to do Y, view 3 to do Z, etc, then the hint parameters should work.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
yellowine wrote:
if ID_COMMAND1 is send by pView1
then I want:
pView2->WelcomeYou();
pView3->MakeDinnerForYou();
...
pViewn->GoFishing()..
I think you might maintain a global stack of messages and handle them individually in the individual views iterating through each message in the stack. Push only those msgs into the stack that you wish to have handlers for in the different views.
My most recent CP article :-
A newbie's elementary guide to spawning processes
www.busterboy.org
|
|
|
|
|
|
Something like this ?
CMyMainWnd::OnCommand1()
{
CMyDoc* pDoc = GetActiveDocument();
POSITION pos = pDoc->GetFirstViewPosition();
while( pos )
{
pView = pDoc->GetNextView( &pos);
if( pView->IsKindOf(RUNTIME_CLASS(CMyView1)))
{
((CMyView1*)pView)->WelcomeYou();
}
....etc
}
}
|
|
|
|
|
Thanks for your idea. I do think this approach works except a maintainence problem. Any time I add a view or I have a new command which will cause a train reaction for other view, I have to add a new handler in the CMyMainFrame or add a new IsKindOf(..) in the while loop, right?
|
|
|
|
|
Or you can send the message to each view ...
//
while(....)
{
pView->SendMessage( );
}
and each view will process the message in his own way.
|
|
|
|
|
Think about overriding OnCmdMsg( )and route the messages yourself.
|
|
|
|
|
I will take this approach and give it a try.
Thanks.
|
|
|
|
|
Hi There,
I have a question about tree controls. If I set a tree item's data to something like a pointer to a class or something, when is the appropriate time to delete the pointer?
This is inside a dialog bar which houses the tree control.
Thanks,
Aaron
|
|
|
|
|
When you receive a TVN_DELETEITEM notification for an element in the tree control. That is by far the best way.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
OK, thanks. I suppose this is sent out both when a single item is deleted from an active tree control and during the destruction of the entire control?
|
|
|
|
|
When you delete the control, TVN_DELETEITEM will be sent for each item in the control. It sounds excessive, but it works just fine.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
Thanks!
|
|
|
|
|
I want to be able to display a toolbar to the users with custom buttons (defined in a settings file - NOT resources) and a custom bitmap (also defined external to the application).
Is this possible to with a CToolBar? I know I can use SetButtons to customize the buttons, but it looks like the only way to set a bitmap into a CToolBar is through LoadBitmap. And I don't think I can use LoadBitmap with a non-resource based bitmap.
Any ideas? I'm trying to do it without writing a custom control... if there's no way, I'll just make 'em static. Just trying to be a bit fancy.
J
|
|
|
|
|
Oi. I'm stupid. Just found CToolBar::SetBitmap().
J
|
|
|
|
|
There are many, many ways of getting a CBitmap or BITMAP from a file. See the relevant areas on codeproject.
good luck
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|