I found beneath code for execute some process without freezing UI. This code is executed when \'Start Work\' button is pressed. And I think users would stop this work by \'S
The simple answer is that you can just call process.Kill()
when the token is canceled:
cancellationToken.Register(() => process.Kill());
But there are two problems with this:
InvalidOperationException
.Dispose()
the CancellationTokenRegistration
returned from Register()
, and the CancellationTokenSource
is long-lived, you have a memory leak, since the registrations will stay in memory as long as the CancellationTokenSource
.Depending on your requirements, and your desire for clean code (even at the cost of complexity) it may be okay to ignore problem #2 and work around problem #1 by swallowing the exception in a catch
.