c# - Callback syncronization between threads -
i want wait on getvalues.get method , return values syncronously caller. serverdataprovider, bookdatafetcher classes external mycode, on have no control. on how achieve appreciated.
namespace test { class program { static void main(string[] args) { getvalues val = new getvalues(); val.get("books", datahandler); console.readkey(); } static void datahandler(ilist<string> values) { } } class getvalues { public delegate void datahandler(ilist<string> values); public event datahandler onreceivedatahandler; public void get(string colname, datahandler handler) { onreceivedatahandler += handler; getdatafromserver svr = new getdatafromserver(); svr.onreceivedatahandler += onreceivedata; svr.getdata(colname); } // callback handler private void onreceivedata(ilist<string> values) { onreceivedatahandler(values); } } class getdatafromserver { internal delegate void datahandler(ilist<string> values); internal event datahandler onreceivedatahandler; internal void getdata(string columnname) { serverdataprovider datafetcher = new serverdataprovider(); datafetcher.onreceivedatahandler += onreceivedata; task.factory.startnew(() => datafetcher.getdatafromserver(columnname)); } // callback handler private void onreceivedata(ilist <string> values) { onreceivedatahandler(values); } } class serverdataprovider { internal delegate void datahandler(ilist<string> values); internal event datahandler onreceivedatahandler; public void getdatafromserver(string columnname) { bookdatafetcher b = new bookdatafetcher(); b.onreceivedatahandler += onreceivedata; task.factory.startnew(() => b.getbookdata(columnname)); } // calback handler private void onreceivedata(ilist<string> values) { onreceivedatahandler(values); } } class bookdatafetcher { internal delegate void datahandler(ilist<string> values); internal event datahandler onreceivedatahandler; public void getbookdata(string col) { thread.sleep(5000); onreceivedatahandler(new list<string> {"book1", "book2"}); } }
}
what asking can achieved using methodimplattribute
[methodimplattribute(methodimploptions.synchronized)] public void methoda()
Comments
Post a Comment