Results 1 to 4 of 4

Thread: [RESOLVED] Shutting down form with backgroundworker

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] Shutting down form with backgroundworker

    Hi,

    So I have this form with a backgroundworker component.
    To properly clean up the form I tried calling backgroundWorker1.CancelAsync() in the Form_Closed event, but it seems to cancel that event. The form doesn't close.
    Without everything is fine.

    Can I assume the component cleans up its own threads at disposal?

    Thanks
    Delete it. They just clutter threads anyway.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Shutting down form with backgroundworker

    In your code in the DoWork event, do you ever check for a pending cancellation?

    If your code doesn't ever exit any loops or whatever then the thread will continue until it's done or externally terminated (without cleanup). I don't know if the BW component terminated it's threads when you dispose of it or not.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Shutting down form with backgroundworker

    The BackgroundWorker raises its DoWork event on a thread pool thread, which means a background thread. The one and only difference between a foreground thread and a background thread is that a background thread will not stop your process exiting, while a foreground thread will. That means that you can simply exit the app and the background task will be terminated.

    That's OK if your background task can reasonably be terminated without any cleanup. If it can't then that's a problem. In that case you would have to stop the app exiting until your background task can respond to the request to cancel and clean up appropriately. In that case, you may be better off implementing your own threading and using a foreground thread instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Shutting down form with backgroundworker

    The form is a reconnection dialog that pauses the application while there is no connection to the server.
    The backgroundworker's job is to check the connection (the check takes no more than a second).
    It's fired by a timer every 5 seconds.
    So basically the connection fails while the form is running.
    When the user closes the dialog the application exits.

    With the slight chance that the worker is canceled while connecting to the database I think the connection object is disposed when the form closes.
    If not, one connection object stays in memory?

    Guess it's running fine then.
    Thanks for the suggestions!
    Delete it. They just clutter threads anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width