Featured post
.net - BeginReceive in a separate thread -
for reason, need beginreceive on separate thread, example :
public void waitfordata() { thread t = new thread(waitfordatathread); t.start(); } public void waitfordatathread() { try { csocketpacket thesocpkt = new csocketpacket(); thesocpkt.thissocket = m_socclient; m_asynresult = m_socclient.beginreceive(thesocpkt.databuffer, 0, thesocpkt.databuffer.length, socketflags.none, ondatareceived, thesocpkt); } catch (socketexception se) { messagebox.show(se.message); } }
but i've received error right after beginreceive call, ondatareceived instantly raised, in event call endreceive method, , error thrown : "the i/o operation has been aborted because of either thread exit or application request".
but if remove separate-thread part (like directly call waitfordatathread(), without going through waitfordata() method), works fine.
if wondering why need create separate thread, because need call beginreceive during event generated different thread different class. pretty same creating new thread sample above, , need make work.
is there way can this??
found answer on msdn (documentation of socket.endreceive):
all i/o initiated given thread canceled when thread exits. pending asynchronous operation can fail if thread exits before operation completes.
you have make sure, thread start socket-operation not exit until done socket i/o.
a workaround use threadpool thread, described here.
- Get link
- X
- Other Apps
Comments
Post a Comment