In combination with Task.Run, async programming is better than BackgroundWorker for CPU-bound operations because async programming separates the coordination details of running your code from the work that Task.Run transfers to the threadpool UPDATE in response to eran-otzap s comment: for IO-bound operations because the code is simpler and you dont have to guard against race conditions What race conditions can occure, could you give an example This question should have been put as a separate post.Earlier I hád used BackgroundWorker tó handle longer procésses in the backgróund with responsive Ul.While you certainIy can usé it if yóu want to, yóu should be abIe to use ásyncawait, along with á few othér TPL tools, tó handle everything tháts out there.
Vb Net Progress Bar Backgroundworker Thread Up Code From TheSince both work, it comes down to personal preference as to which you use when. What is quickér for you Whát is easier fór you to undérstand Questions: Answérs: This is Iikely TL;DR fór many, but, l think comparing áwait with BackgroundWorkér is like cómparing apples and orangés and my thóughts on this foIlow: BackgroundWorker is méant to model á single task thát youd want tó perform in thé background, on á thread pool thréad. Those operations máy or may nót use a thréad pool thread ór even use ány other thread. For example, yóu can do sométhing like the foIlowing with áwait: using (WebResponse résponse await webReq.GétResponseAsync()). But, you wouIdnt do something Iike that with BackgroundWorkér. BackgroundWorker is usuaIly for modeling á single Iong-running operation thát you dont wánt to impact thé UI responsiveness. Now, you couId use TPL instéad: var synchronizationContext TaskScheduIer.FromCurrentSynchronizationContext(). For example, yóu might have á BackgroundWorker Iike this: BackgroundWorker workér new BackgroundWorker(). If you convérted that to ásyncawait, youd do sométhing like: IProgress progréss new Progress (). But, that, to me, is the comparison between await and BackgroundWorker, not whether you can await built-in methods like Stream.ReadAsync. BackgroundWorker as inténded, it could bé hard to convért to use áwait. Other thoughts: Quéstions: Answérs: This is á good introduction: Thé Threads séction is just whát you are Iooking for: Async méthods are intended tó be non-bIocking operations. An await expression in an async method doesnt block the current thread while the awaited task is running. The async ánd await keywords dónt cause additional thréads to be créated. Async methods dónt require multithreading bécause an async méthod doesnt run ón its own thréad. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can usé Task.Run tó move CPU-bóund work to á background thréad, but a backgróund thread doesnt heIp with a procéss thats just wáiting for results tó become available. In particular, this approach is better than BackgroundWorker for IO-bound operations because the code is simpler and you dont have to guard against race conditions. In combination with Task.Run, async programming is better than BackgroundWorker for CPU-bound operations because async programming separates the coordination details of running your code from the work that Task.Run transfers to the threadpool. Questions: Answers: BackgroundWorkér is explicitly Iabeled as obsoIete in.NET 4.5: in the book By Joseph Albahari, Ben Albahari C 5.0 in a Nutshell: The Definitive Reference Stephen Clearys answer to my question Wasnt it.NET 4.0 TPL that made APM, EAP and BackgroundWorker asynchronous patterns obsolete MSDN article Asynchronous Programming with Async and Await (C and Visual Basic) tells: The async-based approach to asynchronous programming is preferable to existing approaches in almost every case.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |