Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:57:57+00:00 2026-06-06T18:57:57+00:00

I have a background worker which calls a function within a separate class. This

  • 0

I have a background worker which calls a function within a separate class. This process may be required to be canceled at any time via a button click from the front end. I have tried using CancelAsync() but this has no effect. The cofunds_downloadfiles is the function which i am calling. How do i go about canceling the process?

TIA.

Private Sub btnProcessdld_Click(sender As System.Object, e As System.EventArgs) Handles btnProcessdld.Click

    Dim cfHelper As New CoFundsHelper

    If btnProcessdld.Text = "Process" Then
        btnProcessdld.Text = "Cancel"

        If chkDailyFiles.Checked = False And chkWeeklyFiles.Checked = False Then
            MessageBox.Show("Please select which files you want to download")
        Else

            lblProgress.Text = "Processing...if you are downloading weekly files this may take a few minutes"
            uaWaitdld.AnimationEnabled = True
            uaWaitdld.AnimationSpeed = 50
            uaWaitdld.MarqueeAnimationStyle = MarqueeAnimationStyle.Continuous
            uaWaitdld.MarqueeMarkerWidth = 60

            _backGroundWorkerdld = New BackgroundWorker
            _backGroundWorkerdld.WorkerSupportsCancellation = True
            _backGroundWorkerdld.RunWorkerAsync()

        End If

    ElseIf btnProcessdld.Text = "Cancel" Then
        btnProcessdld.Text = "Process"
        _backGroundWorkerdld.CancelAsync()
        uaWaitdld.AnimationEnabled = False

    End If

Private Sub StartProcessdld(ByVal sender As Object, _
    ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _backGroundWorkerdld.DoWork

    Dim cfHelper As New CoFundsHelper
    cfHelper.ConnString = PremiumConnectionString
    Dim dateValue As String

    Dim weekly As Boolean = False
    Dim daily As Boolean = False

    If dtePicker.Value IsNot Nothing Then
        dateValue = Format(dtePicker.Value, "yyyyMMdd")

        If chkWeeklyFiles.Checked = True Then
            weekly = True
        End If
        If chkDailyFiles.Checked = True Then
            daily = True
        End If

        cfHelper.Cofunds_DownloadFiles(dateValue, weekly, daily)

    Else
        Throw (New Exception("Date Field is empty"))
    End If
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:57:58+00:00Added an answer on June 6, 2026 at 6:57 pm

    Basically you can do the following:

    • in the DoWork sub, test for cancellationpending property
    • if it’s true then you simply do not call that function and maybe put e.Cancelled = true then check this in the RunWorkerCompleted and decide what you have to do.
    • if you need to cancel it, simply make a Stop() sub in your class that does exactly that – stops the procedure. Then, you simply need to invoke it like

      Me.Invoke(Sub()
                myClass.Stop()
             End Sub)
      
    • you may need to suspend the background worker until the call from your main thread has returned. You do this using semaphores: Private chk As New Semaphore(1,1,"checking1") You put this as a global variable to both your main thread and the background worker.

    • in the backgroundworker_doWork you use the semaphore like chk.WaitOne() AFTER the line that need to execute.
    • in the method of your class, when it has finished with computing you put a.Release

    The semaphore is only needed if you need to make sure you wait for a result. It kind of defeats the purpose of multithreading but you can perform other actions in the worker before waiting for the main thread (like starting another thread with something else etc).

    Other than that invoking a stop method should be enough. Sorry that i haven’t had time to analyze your code but i hope i put you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a background worker which can be cancelled. The normal flows interrupt itself
I have a background worker that stops after 100 iterations. Like this: BackgroundWorker bgWorker
lets say i have a background worker in a class that perform db query
I have a backgroundworker which calls a method in the DoWork event. this method
I am a little confused. I have a background worker which in its dowork
I have got a C# user control, which has got it's own background worker
I have a background worker thread that is constantly syncing data to/from a remote
Hi all I have a BAckground worker and a Datatable. I have a timer
Scenario I have a background worker in my application that runs off and does
Hi all i have a background worker thread and some unmanaged code dlls ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.