Events and threads in a socket based program
I'm working on a library for the .NET messenger service.
It has a connection to the main notification server, and a conne开发者_开发技巧ction per instant messaging session. All handled using the Begin/End asynchronous methods.
At present, events for the library user (ie.. MessageReceieved) are called inside the read callback thread (albeit traversing through a few different layers.. message parsing and what not).
This is fine, it just means that the library user has to be careful. For instance, a blocking operation inside one of the event handlers will stop any data from being received.
Is this an acceptable/the standard way to do things? I could easily raise the events on a threadpool thread if necessary.
Eventually I decided that this is acceptable. You'll see the same sort of problem with Windows Forms. For example, if you perform a blocking operation in a Clicked event handler, the whole form locks up.
精彩评论