Search code examples
c#windowsformstaskabort

How to Abort Current task in windows form application c#


I want to provide a button which cancels the present task,

Let's say I want to perform the following task, which takes approximately 30 minutes

 private void CaptureSignal()
  {
        // Capture Signal Code ......
  }

I need to display a button which cancels the current task(any time during the process).

any suggestions...?

Thanks in advance...:)


Solution

  • If your capture is done inside main thread, try this:

    bool abort = false;
    
    private void CaptureSignal()
    {
        while (!abort) {
        {
            // Capture Signal Code ......
        }
    }
    
    private void OnButtonClick(.............)
    {
        abort = true;
    }
    

    If you're using a BackgroundWorker you can send a cancellation request.