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 7195863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:39:57+00:00 2026-05-28T20:39:57+00:00

I’m seeing some strange behavior where the RunWorkerCompleted event for one of two threads

  • 0

I’m seeing some strange behavior where the RunWorkerCompleted event for one of two threads I start isn’t being called depending on how I call them. Check out the code below, and the two methods of firing the threads, good() and bad().

Public Class Form1

  Private WithEvents bw As System.ComponentModel.BackgroundWorker
  Private WithEvents bw2 As System.ComponentModel.BackgroundWorker
  Private starts As Integer = 0

  Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
    starts += 1
    Threading.Thread.Sleep(e.Argument)
  End Sub

  Private Sub bw_Completed(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
    MessageBox.Show("Ending " + starts.ToString())
  End Sub

  Private Sub bad()
    bw = New System.ComponentModel.BackgroundWorker()
    bw.RunWorkerAsync(5000)

    Threading.Thread.Sleep(500)

    bw = New System.ComponentModel.BackgroundWorker()
    bw.RunWorkerAsync(5)
  End Sub

  Private Sub good()
    bw2 = New System.ComponentModel.BackgroundWorker()
    AddHandler bw2.DoWork, AddressOf bw_DoWork
    AddHandler bw2.RunWorkerCompleted, AddressOf bw_Completed
    bw2.RunWorkerAsync(500)

    bw2 = New System.ComponentModel.BackgroundWorker()
    AddHandler bw2.DoWork, AddressOf bw_DoWork
    AddHandler bw2.RunWorkerCompleted, AddressOf bw_Completed
    bw2.RunWorkerAsync(5)
  End Sub

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'good()
    bad()
  End Sub
End Class

In both cases the DoWork event is called for both threads. But in the bad() case, only the second thread fires the RunWorkerCompleted event. This is obviously due to the two different ways I’m using VB to handle events here. I’m looking for an explanation of this behavior, preferably with a link to some documentation that could help me understand how these events are being handled in VB better. It seems strange to me that just reusing a variable name here seems to either dispose of the thread before it’s done or else just make it stop firing events.

  • 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-05-28T20:39:58+00:00Added an answer on May 28, 2026 at 8:39 pm

    Where is this automatic unsubscribing documented?

    In the Visual Basic Language Specification, a document you can download from Microsoft. Chapter 9.6.2 “WithEvents Variables” says this:

    The implicit property created by a WithEvents declaration takes care of hooking and unhooking the relevant event handlers. When a value is assigned to the variable, the property first calls the remove method for the event on the instance currently in the variable (unhooking the existing event handler, if any). Next the assignment is made, and the property calls the add method for the event on the new instance in the variable (hooking up the new event handler).

    The bolded phrase describes the behavior you see. It is rather important it works that way. If it didn’t then you could never unsubscribe from an event and the event subscriptions would pile on without limit.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.