c# - Task locking UI in WPF -
i have been trying learn process of using task in wpf , have ran snag more due lack of experience. when executing asychronous call dataservice method "getfuturework" ui thread becomes unresponsive. code can seen below. note project uses mvvm , variable "worklist" observable collection used listiview's itemsource.
private async void loadwork() { worklist = await _dataservice.getfuturework("usernamehere"); }
dataservice task
public async task<ienumerable<futurework>> getfuturework(string username) { using (_db = new dataentities()) { var worklist = await (from items in _db.repair_check_in_tables items.location == username && items.completed == "n" select new futurework { formatteddate = items.estshipdate.tostring(), serviceid = items.service_id, imagepath = @"\\192.168.5.50\photos$\" + items.service_id + "p1.bmp", priority = items.priority }).tolistasync(); return worklist; } }
you can't call async function window constructor, others have mentioned, you'll need way.
you use window_loaded event instead of constructor.
private async void window_loaded(object sender, routedeventargs e) { await loadwork(); }
then hook up:
<window x:class="asyncwindow.mainwindow" ... loaded="window_loaded">
edit:
if prefer stick mvvm pattern, alternate solution posted here.
Comments
Post a Comment