c# - How to use other parts of program when loading data -
don't know how inquire question. assume related multitasking or similar.
in form1 there menu button opening form2. in form2 load event need load info populate datagrid.
im using entityframework.
when press menu button, form2 shown without controls until info loaded, , cannot utilize menu on form1 because stuck in button opening form2.
is there way utilize other parts of programme while loading data, illustration click button opening calculator (or not related data) in .net 4.5.
the problem approach not taking consideration winforms using single ui thread, when effort load info synchronously in sec form using ef, blocking ui thread, , why sec form not appear immediately.
an obvious solution unblock ui thread, able paint sec form while info loading in background thread, , display info when ready.
one possible approach override onload method in form2 class. can utilize tpl task load ef info in background in async fashion, , synchronize info ui thread.
the simplest code can think of utilize async/await tpl task:
protected override async void onload(eventargs e) { base.onload(e); var result = await task.run(() => _ctx.myentities.where(a => a.name.contains("mx")).tolistasync()); // stuff info txtinfo.text = "loaded data"; }
the _ctx
variable ef dbcontext of application.
if don't tpl approach above, can take @ older api's backgroundworker
load info in background:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.110%29.aspx
c# .net entity-framework
No comments:
Post a Comment