What is the C#/.NET 4.0+ way to implement a cancellable background thread? -


before .net 4.0, understanding if 1 wanted start thread work cancelled, 1 start backgroundworker. .net 4 brought tap model, , whole bunch of new async , threading stuff. backgroundworker still way it?

note async (and corollary keyword await) both .net 4.5, , not available in 4.0. if want use tasks @ all, highly recommend upgrading .net 4.5. in 4.0, however, there areseveral overloaded methods optionally take cancellationtokens can used cancel running work.

edit: pointed out in comments, there are ways async/await on .net 4.0 - need less work , have more library support in 4.5. not recommend without dire need xp support.

as of .net 4.5, backgroundworker class not marked [obsolete], there's no reason shouldn't continue using directly.

the thread class contains direct .abort() method, can used create worker can cancelled. note abort happens via throwing threadabortexception in running thread, allowing catch statement still perform cleanup in event of thread abortion.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -