c# - Task causing strange behavior in form_load event -
i have task runs in form_load event of usercontrol in winforms:
private void ucdeviceinsert_load(object sender, eventargs e) { system.threading.tasks.task gettbox = system.threading.tasks.task.run(async () => { await averylongrunningproccess(); }); pbimage.image = properties.resources.remove; gettbox.wait(); pbimage.image = properties.resources.insert; btnnext.visible = true; tmrdeviceinsert.enabled = true; tmrdeviceinsert.start(); } private void tmrdeviceinsert_tick(object sender, eventargs e) { next(); }
i change image of picture box inform user progress of long running process. part works fine, button doesn't show, , timer never starts. i've stepped through code, , can confirm running without problems, makes more baffling. ideas causing issue?
task.run
pushing cpu-intensive work off ui thread. since you're calling asynchronous method, suspect it's not cpu-intensive.
so, can use async
, await
:
private async void ucdeviceinsert_load(object sender, eventargs e) { pbimage.image = properties.resources.remove; await averylongrunningproccess(); pbimage.image = properties.resources.insert; btnnext.visible = true; tmrdeviceinsert.enabled = true; tmrdeviceinsert.start(); }
note @ await
, ui shown , user can interact (that's point).
Comments
Post a Comment