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 task
s @ all, highly recommend upgrading .net 4.5. in 4.0, however, there areseveral overloaded methods optionally take cancellationtoken
s 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
Post a Comment