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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현