c# - How can I use Await Task.Delay without labeling all methods Async? -
i'm working on replacing old code uses threads .net 4.5 task based system.
i've replaced threads tasks, next i'm working on replacing thread.sleep calls await task.delay. problems i'm having await can't called within synclock section , every method uses await task.delay instead needs labeled async.
i can around synclock issue have few blocks of , can replaced better code. however, having label each method async causing problems. means have label method calls method async , on down chain. methods going async.
is how should done when using task based approach or doing wrong?
edit 1: posting example code
public async function delay(milliseconds integer, optional initial boolean = false, optional silent boolean = false, optional modifier double = 0.3, _ optional task task = nothing, optional exitontaskstop boolean = true) tasks.task(of boolean) dim outputbufferkey string = nothing if task isnot nothing if task.stoprequested return true outputbufferkey = task.outputbufferkey end if if initial milliseconds = rand.next(0, milliseconds) elseif modifier > 0 dim minvalue integer = cint(milliseconds * (1 - modifier)) dim maxvalue integer = cint(milliseconds * (1 + modifier)) if minvalue < maxvalue milliseconds = rand.next(minvalue, maxvalue + 1) end if if debugmode andalso not silent dim lengthstring string = nothing select case milliseconds case < 60 * 1000 : lengthstring = math.round(milliseconds / 1000, 1) & " seconds" case < 60 * 60 * 1000 : lengthstring = math.round(milliseconds / (60 * 1000), 1) & " minutes" case else : lengthstring = math.round(milliseconds / (60 * 60 * 1000), 1) & " hours" end select output(outputbufferkey, "sleeping " & lengthstring) end if if exitontaskstop andalso task isnot nothing try await tasks.task.delay(milliseconds, task.cancellationtoken.token) catch ex operationcanceledexception return true end try else await tasks.task.delay(milliseconds) end if return false end function
i need delay many reasons , method called in number of other methods. give couple of examples have code uses openpop dll check inbox message arrive, if it's not there on first check need wait before check again, have loop , sleep if nothing has been found. example need make httpwebrequest check file, if contents haven't changed want again in 30 seconds.
i suppose using timers better idea? if so, proper use of task.delay?
the methods need await, should marked async, not everything. if have simple function adds var1 + var2
, ridiculous make async. being said, end making lot of thing async because call calls needs awaited.
based on discussion here, might better off timers leads other question, "what proper use of task.delay"? solely use can wait ui finish doing. example, make call database , start ui transformation maybe gradually show new part of screen on next 1/2 second. don't want fill in results during 1/2 second because creates jittery screen wait until done. easy way make call database, , create delay use whenall
wait last 1 finish. 1/2 second database.
as side note, can call synchronous methods using async work in background well. easier transition threading since changed of code, don't want here that.
Comments
Post a Comment