c# - await for another method -
i think need async await program can't figure out how it. getdata method need send request socket, , socket data same method. need pass data somehow onreceive initial getdata method. possible implement await async tasks here? here simplified scheme:
asynccallback m_pfnlookupcallback; socket m_sock; public void getdata() { string data; if (condition) data = getdatafromcache(); else data = getdatafromnet(); /// !need await socket data here //will process data here. } public string getdatafromnet() { m_sock.send(szcommand, ibytestosend, socketflags.none); waitfordata("s1"); } public void waitfordata(string ssocketname) { m_pfnlookupcallback = new asynccallback(onreceive); m_sock.beginreceive(m_szlookupsocketbuffer, 0, m_szlookupsocketbuffer.length, socketflags.none, m_pfnlookupcallback, ssocketname); } private void onreceive(iasyncresult asyn) { int ireceivedbytes = 0; ireceivedbytes = m_socklookup.endreceive(asyn); string sdata = encoding.ascii.getstring(m_szlookupsocketbuffer, 0, ireceivedbytes); //what need }
p.s. avoid changing socket work if possible because used in other parts of program.
you can turn old iasyncresult pattern (apm) async await quite easily. here's example of socket call:
var bytecount = await task.factory.fromasync( (callback, s) => { return clientsocket.beginreceive( m_szlookupsocketbuffer, 0, cm_szlookupsocketbuffer.length, socketflags.none, callback, ssocketname); }, result => clientsocket.endreceive(result), null);
Comments
Post a Comment