c# - Issue using Task.Factory.FromAsync -


i enjoyed new c# 5's async , await , want set 1 of previous tcp application used async whay (not async & await, in fact begin*, end*)

in application every message have response, every time use beginsend, receive message related message first sent. (lets suppose command , it's report)

i want add new function called requestasync send message , wait till response comes. let me show usage

string response = await requestasync("hi"); 

on occasion, send "hi" message , code waits till response related comes.

i had problems using task.factory.fromasync, tried following code, want make sure performance same taskfactory way.

public async task<irequestresult> sendrequestasync(irequest request, string message) {         irequestresult result = beginsendrequest(request, message);          while (!result.iscompleted)         {             await task.delay(1);         }          return result; } 

and beginrequest own method.

sincerely yours, peyman mortazavi

no, that's going pretty awful in terms of efficiency - you'll waking , requiring rescheduling every 5 milliseconds. you've added latency while sleep between cycles well.

you should go trying use task.factory.fromasync - haven't told went wrong when tried that, it's hard fix it... it's possible haven't implemented asynchronous programming model pattern correctly start with. (the fact you're not providing callback beginsendrequest method suggestive of that.)

i hope real code doesn't have catch {} either...


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -