Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

wxSocket Event Notification in a Thread

222 views
Skip to first unread message

Nathan Stokes

unread,
Apr 13, 2004, 9:34:39 AM4/13/04
to
I am working on an application using wxWindows on MSW, with the MFC
compatibility turned on so as to get Direct Draw support. I am trying to
set up a thread to monitor a wxSocket for incoming data using a
wxSocketEvent. My thread class inherits wxEventHandler and wxThread. I
declare an event table, register the event with wxSocket. My event handling
function is called when I get incoming data, but the context is wrong. The
object context of the handler when it is called is not the same context as
the thread that created the socket. Are event handlers thread safe when
implemented as I have?

Thanks,

Nathan Stokes
Research Engineer
System Technology Center
Syracuse Research Corporation
6225 Running Ridge Rd
North Syracuse, NY 13212
sto...@syrres.com
315.452.8125

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-u...@lists.wxwidgets.org
For additional commands, e-mail: wx-use...@lists.wxwidgets.org

Łukasz Michalski

unread,
Apr 13, 2004, 9:42:15 AM4/13/04
to
> compatibility turned on so as to get Direct Draw support. I am trying to
> set up a thread to monitor a wxSocket for incoming data using a
> wxSocketEvent. My thread class inherits wxEventHandler and wxThread. I
> declare an event table, register the event with wxSocket. My event
> handling function is called when I get incoming data, but the context is
> wrong. The object context of the handler when it is called is not the same
> context as the thread that created the socket. Are event handlers thread
> safe when implemented as I have?
>

In wxWindows all event handlig occurs always in main thread.

Regards,
Łukasz

Kevin Hock

unread,
Apr 13, 2004, 9:43:02 AM4/13/04
to
I tried to do double-inheritance as you did once and found that it simply
didn't work (same problems as you have), although I seem to recall something
about this being discussed in the past. Did you try the archive?
http://lists.wxwidgets.org/cgi-bin/search.cgi

I ended up deriving just from wxThread and creating a different class that
derived from wxEvtHandler.

Kevin

----- Original Message -----
From: "Nathan Stokes" <sto...@syrres.com>
To: "Wx-Users" <wx-u...@lists.wxwidgets.org>
Sent: Tuesday, April 13, 2004 9:31 AM
Subject: wxSocket Event Notification in a Thread


> I am working on an application using wxWindows on MSW, with the MFC
> compatibility turned on so as to get Direct Draw support. I am trying to
> set up a thread to monitor a wxSocket for incoming data using a
> wxSocketEvent. My thread class inherits wxEventHandler and wxThread. I
> declare an event table, register the event with wxSocket. My event
handling
> function is called when I get incoming data, but the context is wrong.
The
> object context of the handler when it is called is not the same context as
> the thread that created the socket. Are event handlers thread safe when
> implemented as I have?
>
> Thanks,
>
> Nathan Stokes

Nathan Stokes

unread,
Apr 13, 2004, 11:56:35 AM4/13/04
to
I can't seem to search the archive at this moment. However I found a
solution to my problem with the sockets. After creating the socket in my
thread, I use the SetClientData function to pass in a pointer to my thread
object. In my event handler, which is of the thread object type, I use the
GetClientData to get said pointer back. I can then use this pointer to call
the functions in my thread within the proper context. This is rather
circular looking at it, but it gets the job done.

Aj Lavin

unread,
Apr 13, 2004, 8:48:23 PM4/13/04
to
On Tue, 2004-04-13 at 08:54, Nathan Stokes wrote:
> I can't seem to search the archive at this moment. However I found a
> solution to my problem with the sockets. After creating the socket in my
> thread, I use the SetClientData function to pass in a pointer to my thread
> object. In my event handler, which is of the thread object type, I use the
> GetClientData to get said pointer back. I can then use this pointer to call
> the functions in my thread within the proper context. This is rather
> circular looking at it, but it gets the job done.

I use a similar trick in my ThreadHandler code to process events in a
thread:

http://www.ecto.us/code.html

The basica idea is to override wxEvtHandler::ProcessEvent in the
thread-based event handler class. The implementation pushes the event
onto a threadsafe message queue. The thread main loop can be written to
consume events as they become available in the queue, calling
wxEvtHandler::ProcessEvent on each one.

So an event that gets posted to the thread-based event handler class,
either through wxPostEvent or the ProcessEvent event method, gets added
to your message queue by the main thread, then dispatched to the correct
event handler in the handler's thread.

My first implementations used multiple inheritance, and that strategy
seemed to work ok. But now that I am using thread pools I, I just derive
my ThreadHandler class from wxEvtHandler.

Hope that helps,

Aj

0 new messages