Terminating function execution if a context is cancelled

Function calls and goroutines cannot be terminated from the caller, the functions and goroutines have to support the cancellation, often via a context.Context value or a done channel. In either case, the functions are responsible to check / monitor the context, and if cancel is requested (when the Context’s done channel is closed), return early. … Read more

How to use the CancellationToken without throwing/catching an exception?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

How to stop a DispatchWorkItem in GCD?

GCD does not perform preemptive cancelations. So, to stop a work item that has already started, you have to test for cancelations yourself. In Swift, cancel the DispatchWorkItem. In Objective-C, call dispatch_block_cancel on the block you created with dispatch_block_create. You can then test to see if was canceled or not with isCancelled in Swift (known … Read more

How to use the CancellationToken property?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

How cancel the execution of a SwingWorker?

as jzd montioned, there is method cancel(boolean mayInterruptIfRunning); for exapmle EDIT: with cancel(true); you have to (always) cautgh an exception java.util.concurrent.CancellationException import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; public class SwingWorkerExample extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private final JButton startButton, stopButton; private JScrollPane scrollPane = new JScrollPane(); … Read more

Promise – is it possible to force cancel a promise

In modern JavaScript – no Promises have settled (hah) and it appears like it will never be possible to cancel a (pending) promise. Instead, there is a cross-platform (Node, Browsers etc) cancellation primitive as part of WHATWG (a standards body that also builds HTML) called AbortController. You can use it to cancel functions that return … Read more