need help accessing data coming off a callback C# .NET -
i'm @ wits end trying solve issue.
i have function in class such
public class receivedata { dataprovider provider = new dataprovider(); public void responsedata() { foreach(string anitem in thelist) { // alldata function declared in class dataprovider string result = provider.alldata(anitem); } //do } }
that's simple. however, if alldata function had make async function calls data?
meaning, say
public class dataprovider { myserviceclient client = new myserviceclient(); public string alldata (string myitem) { client.formatteddatacompleted += new eventhandler<formatteddatacompletedeventargs>(client_formatteddatacompleted); client.formatteddataasync(myitem); } void client_formatteddatacompleted(object sender, formatteddatacompletedeventargs e) { // here's response comes back. }
as can see, cant call alldata function , directly data back.
so, have in responsedata function make sure call allitem function, , data callback. notice there's loop in function, need parameters have sent through loop gets respective response.
one approach tried using autoresetevent class.
i defined handler autoresetevent handle = new autoresethandle(false);
then add handle.waitone() right after async call. , on callback, added handle.set();
however, applications stuck @ handle.waitone(). can't see why happens.
i have theoritical idea see if have event raiser on callback, , eventlistener in recievedata class, , if 2 communicate, data. i've spent time trying learn more event handlers, haven't gotten hang of it.
so have other approach, ideas? thanks!
welcome asynchronous model. have re-think how call methods when go multithreaded.
you have options:
split method in you're making call 2 halves; in first half, call alldata, else can possibly without having response object, , exit current method. when async call completes, it'll call handler, should perform operations original method require response.
in event handler async call completion, set property indicates call completed, , put data out on property somewhere well. in function kicks off call, @ point cannot continue execution until call completes, wait in while loop call complete polling property , breaking once indicates call has been completed (you'll want thread.yield() in loop you'll call when haven't gotten response yet, make sure aren't blocking other threads doing more important things waiting)
wrap asynchronous call in class exposes synchronous method. wrapper class have similar logic integrating async call method. make call, wait response, , return response if normal synchronous method call.
remember asynchronous call giving calling thread opportunity other work while call performed. take full advantage of if @ possible.
Comments
Post a Comment