Background Worker cancel async not workingbackground worker class cancellation, sets cancellation pending flag but doesn't quitCancelling background-workerC# - Background Workers?How to cancel a function which is called by a background worker?How and when to use ‘async’ and ‘await’Stopping background workerStopping and then starting the same background worker in quick successionvb.net background worker cancel not workingOn a blocking background worker and Application.DoEventsCannot cancel a background worker running a SQL query

Scam? Checks via Email

Should I put my name first or last in the team members list?

Adding a (stair/baby) gate without facing walls

Prepare a user to perform an action before proceeding to the next step

How to get Planck length in meters to 6 decimal places

Stationing Callouts using VBScript Labeling in ArcMap?

How to innovate in OR

What to expect in a jazz audition

Create two random teams from a list of players

What is my clock telling me to do?

Patio gate not at right angle to the house

Avoiding Implicit Conversion in Constructor. Explicit keyword doesn't help here

What parameters are to be considered when choosing a MOSFET?

UX writing: When to use "we"?

How did Biff return to 2015 from 1955 without a lightning strike?

How do discovery writers hibernate?

Why don't short runways use ramps for takeoff?

Gold Battle KoTH

Derivative is just speed of change?

Best Ergonomic Design for a handheld ranged weapon

Help me, I hate squares!

Why would an invisible personal shield be necessary?

Should 2FA be enabled on service accounts?

Can starter be used as an alternator?



Background Worker cancel async not working


background worker class cancellation, sets cancellation pending flag but doesn't quitCancelling background-workerC# - Background Workers?How to cancel a function which is called by a background worker?How and when to use ‘async’ and ‘await’Stopping background workerStopping and then starting the same background worker in quick successionvb.net background worker cancel not workingOn a blocking background worker and Application.DoEventsCannot cancel a background worker running a SQL query






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Program Logic



I'm running a background worker for reading and writing files. For checking if cancel button is clicked, I've created a method called "check_bgWorkerCancelled()" and this is supposed to handle all the events if cacellation is pending.



Problem



When I click the cancel button, the worker keeps running. When I click back the start button, the old process keeps running.



Code: For checking cancellation



' Method: To check is cancellation is pending
Private Sub check_bgWorkerCancelled()
' check if cancellation is pending
If bgWorker_Scanner.CancellationPending = True Then
' background worker cancel asynchronous operating
If bgWorker_Scanner.IsBusy Then
Try 'try to
' cancel the threads
bgWorker_Scanner.CancelAsync()
' dispose the background worker
bgWorker_Scanner.Dispose()
Catch ex As Exception 'if exception
' invoke error
utils.invoke_msg(3, "Worker Error", ex.Message.ToString)
Me.Close() 'close form
End Try
End If
isScanning = False 'set scanning to false
Try
' invoke to bypass illegal cross threading UI update
BeginInvoke(CType(Sub()
progressBar1.Value = 0
txtStatus.Text = "Cancelled"
txtCalmDown.Text = ""
btnToggleScan.Image = Image.FromFile(Application.StartupPath & "/res/malware_scanner/rocket.png")
End Sub, MethodInvoker))
Catch ex As Exception : End Try
Else
Exit Sub
End If
End Sub


Tried Solutions



I've tried the following solutions, but none worked:



Tried to check for cancellation inside DoWork event



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
End If
End Sub


Tried to check for cancellation and collect garbage



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
End If
End Sub


Tried to do the above inside Try-Catch statements



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
If bgWorker_Scanner.IsBusy Then
Try
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End If
End If
End Sub


Debug Image



Is there any way to end the background worker without having to close the form completely? Any help is appreciated.










share|improve this question
























  • If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

    – Jimi
    Mar 26 at 22:53












  • @Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

    – newbieCoder
    Mar 26 at 23:52











  • You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

    – jmcilhinney
    Mar 26 at 23:58











  • I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

    – Jimi
    Mar 27 at 0:02

















1















Program Logic



I'm running a background worker for reading and writing files. For checking if cancel button is clicked, I've created a method called "check_bgWorkerCancelled()" and this is supposed to handle all the events if cacellation is pending.



Problem



When I click the cancel button, the worker keeps running. When I click back the start button, the old process keeps running.



Code: For checking cancellation



' Method: To check is cancellation is pending
Private Sub check_bgWorkerCancelled()
' check if cancellation is pending
If bgWorker_Scanner.CancellationPending = True Then
' background worker cancel asynchronous operating
If bgWorker_Scanner.IsBusy Then
Try 'try to
' cancel the threads
bgWorker_Scanner.CancelAsync()
' dispose the background worker
bgWorker_Scanner.Dispose()
Catch ex As Exception 'if exception
' invoke error
utils.invoke_msg(3, "Worker Error", ex.Message.ToString)
Me.Close() 'close form
End Try
End If
isScanning = False 'set scanning to false
Try
' invoke to bypass illegal cross threading UI update
BeginInvoke(CType(Sub()
progressBar1.Value = 0
txtStatus.Text = "Cancelled"
txtCalmDown.Text = ""
btnToggleScan.Image = Image.FromFile(Application.StartupPath & "/res/malware_scanner/rocket.png")
End Sub, MethodInvoker))
Catch ex As Exception : End Try
Else
Exit Sub
End If
End Sub


Tried Solutions



I've tried the following solutions, but none worked:



Tried to check for cancellation inside DoWork event



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
End If
End Sub


Tried to check for cancellation and collect garbage



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
End If
End Sub


Tried to do the above inside Try-Catch statements



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
If bgWorker_Scanner.IsBusy Then
Try
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End If
End If
End Sub


Debug Image



Is there any way to end the background worker without having to close the form completely? Any help is appreciated.










share|improve this question
























  • If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

    – Jimi
    Mar 26 at 22:53












  • @Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

    – newbieCoder
    Mar 26 at 23:52











  • You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

    – jmcilhinney
    Mar 26 at 23:58











  • I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

    – Jimi
    Mar 27 at 0:02













1












1








1








Program Logic



I'm running a background worker for reading and writing files. For checking if cancel button is clicked, I've created a method called "check_bgWorkerCancelled()" and this is supposed to handle all the events if cacellation is pending.



Problem



When I click the cancel button, the worker keeps running. When I click back the start button, the old process keeps running.



Code: For checking cancellation



' Method: To check is cancellation is pending
Private Sub check_bgWorkerCancelled()
' check if cancellation is pending
If bgWorker_Scanner.CancellationPending = True Then
' background worker cancel asynchronous operating
If bgWorker_Scanner.IsBusy Then
Try 'try to
' cancel the threads
bgWorker_Scanner.CancelAsync()
' dispose the background worker
bgWorker_Scanner.Dispose()
Catch ex As Exception 'if exception
' invoke error
utils.invoke_msg(3, "Worker Error", ex.Message.ToString)
Me.Close() 'close form
End Try
End If
isScanning = False 'set scanning to false
Try
' invoke to bypass illegal cross threading UI update
BeginInvoke(CType(Sub()
progressBar1.Value = 0
txtStatus.Text = "Cancelled"
txtCalmDown.Text = ""
btnToggleScan.Image = Image.FromFile(Application.StartupPath & "/res/malware_scanner/rocket.png")
End Sub, MethodInvoker))
Catch ex As Exception : End Try
Else
Exit Sub
End If
End Sub


Tried Solutions



I've tried the following solutions, but none worked:



Tried to check for cancellation inside DoWork event



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
End If
End Sub


Tried to check for cancellation and collect garbage



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
End If
End Sub


Tried to do the above inside Try-Catch statements



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
If bgWorker_Scanner.IsBusy Then
Try
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End If
End If
End Sub


Debug Image



Is there any way to end the background worker without having to close the form completely? Any help is appreciated.










share|improve this question














Program Logic



I'm running a background worker for reading and writing files. For checking if cancel button is clicked, I've created a method called "check_bgWorkerCancelled()" and this is supposed to handle all the events if cacellation is pending.



Problem



When I click the cancel button, the worker keeps running. When I click back the start button, the old process keeps running.



Code: For checking cancellation



' Method: To check is cancellation is pending
Private Sub check_bgWorkerCancelled()
' check if cancellation is pending
If bgWorker_Scanner.CancellationPending = True Then
' background worker cancel asynchronous operating
If bgWorker_Scanner.IsBusy Then
Try 'try to
' cancel the threads
bgWorker_Scanner.CancelAsync()
' dispose the background worker
bgWorker_Scanner.Dispose()
Catch ex As Exception 'if exception
' invoke error
utils.invoke_msg(3, "Worker Error", ex.Message.ToString)
Me.Close() 'close form
End Try
End If
isScanning = False 'set scanning to false
Try
' invoke to bypass illegal cross threading UI update
BeginInvoke(CType(Sub()
progressBar1.Value = 0
txtStatus.Text = "Cancelled"
txtCalmDown.Text = ""
btnToggleScan.Image = Image.FromFile(Application.StartupPath & "/res/malware_scanner/rocket.png")
End Sub, MethodInvoker))
Catch ex As Exception : End Try
Else
Exit Sub
End If
End Sub


Tried Solutions



I've tried the following solutions, but none worked:



Tried to check for cancellation inside DoWork event



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
End If
End Sub


Tried to check for cancellation and collect garbage



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
End If
End Sub


Tried to do the above inside Try-Catch statements



Private Sub bgWorker_Scanner_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker_Scanner.DoWork
If bgWorker_Scanner.CancellationPending = True Then
If bgWorker_Scanner.IsBusy Then
Try
e.CancelAsync()
bgWorker_Scanner.dispose()
GC.Collect()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End If
End If
End Sub


Debug Image



Is there any way to end the background worker without having to close the form completely? Any help is appreciated.







.net vb.net backgroundworker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 22:33









newbieCodernewbieCoder

486 bronze badges




486 bronze badges















  • If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

    – Jimi
    Mar 26 at 22:53












  • @Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

    – newbieCoder
    Mar 26 at 23:52











  • You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

    – jmcilhinney
    Mar 26 at 23:58











  • I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

    – Jimi
    Mar 27 at 0:02

















  • If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

    – Jimi
    Mar 26 at 22:53












  • @Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

    – newbieCoder
    Mar 26 at 23:52











  • You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

    – jmcilhinney
    Mar 26 at 23:58











  • I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

    – Jimi
    Mar 27 at 0:02
















If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

– Jimi
Mar 26 at 22:53






If bgWorker_Scanner.CancellationPending Then 'Do something e.Cancel = True End If. Call [BackGroundWorker].CancelAsync() from the outside.

– Jimi
Mar 26 at 22:53














@Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

– newbieCoder
Mar 26 at 23:52





@Jimi I called the CancelAsync() from a button. Now there seems to be a different problem for some reason.. There is the waiting cursor icon and the button is disabled by default. I didn't set this, but I guess Windows is doing it automatically.. I can't access the button unless the whole operation is finished :'(

– newbieCoder
Mar 26 at 23:52













You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

– jmcilhinney
Mar 26 at 23:58





You clearly do not know how to use a BackgroundWorker properly. Go here to learn. Post #19 contains an example of cancellation.

– jmcilhinney
Mar 26 at 23:58













I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

– Jimi
Mar 27 at 0:02





I hope you're not still trying to dispose of the BackGroundWorker inside DoWork or calling GC.Collect(). If you're in a loop when CancelAsync is called, exit the loop (after e.Cancel = True).

– Jimi
Mar 27 at 0:02












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367133%2fbackground-worker-cancel-async-not-working%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367133%2fbackground-worker-cancel-async-not-working%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript