Logo

Programming-Idioms

  • C#
  • Pascal

Idiom #229 Cancel an operation

Interrupt an ongoing processing p.

uses Process;
var
  P: TProcess;
begin
  ...
  P.Execute;
  //do something else
  P.Terminate(0);
end.

0 wil be the exitcode of the terminated process.
import "context"
ctx, cancel := context.WithCancel(context.Background())
go p(ctx)

somethingElse()

cancel()

Pass a Context to p and execute p.
p is responsible for shutting down gracefully when ctx is canceled

New implementation...
< >
programming-idioms.org