.net - In WinForms.NET, how to properly update the UI in response to a background Task -


i use following code

var uischeduler = taskscheduler.fromcurrentsynchronizationcontext(); cancellationtoken cancellationtoken = cancellationtokensource.token; const taskcontinuationoptions continuationoptions = taskcontinuationoptions.onlyonrantocompletion;  task<string> task = task<string>.factory.startnew(() => getsometext(), cancellationtoken); task.continuewith(t => label.text = t, cancellationtoken, continuationoptions, uischeduler); 

to update ui after background task. if form closed before task finished, update done anyway. there best practice how avoid this? controls begin bug in response using them when form gone. check "disposing" , "isdisposed" avoid i'm not sure if right way , whether there may other threading problems / race conditions that. thx help!

checking form.isdisposed on ui thread before updating ui way go, , avoid race conditions.

if handle formclosing event suggested john willemse, able request cancellation of task - there can still race condition means task complete , update ui.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -