|
-
Apr 22nd, 2016, 10:57 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Background Workers
Hey all!
I've got questions about background workers, how they work and how to use them.
- What do I use background workers for?
- Can background workers freeze the program?
- Will having multiple background workers slow down the program?
- If the form is closed, will there be errors if the background worker is in the middle of a task?
- Can I put time consuming code in a background worker on a timer so it does it every N seconds? But what happens if it is not finished when it is called again?
Lastly, why can I not put this code in a background worker? It gives me the error: Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
Code:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim N As UInt32 = 0
Dim L1 As New Label With {.Text = "0", .Location = New Point(0, 0), .Visible = True, .Enabled = True}
Controls.Add(L1)
Do
N += 1
L1.Text = N
Loop
End Sub
Thanks in advance!
~Nic
-
Apr 23rd, 2016, 01:34 AM
#2
Hyperactive Member
Re: Background Workers
- What do I use background workers for? - Typically these are used for long running tasks that you don't want to hold up the main UI thread waiting on.
- Can background workers freeze the program? - They will not cause the UI thread to freeze, but they can themselves freeze or get tied up. Say if you have infinite loop code in the BGWorker.
- Will having multiple background workers slow down the program? - Again they shouldn't impact the main UI thread. There is one caveat though, if a BG thread eats up 100% of the CPU cycles it will slow EVERYTHING down on your computer.
- If the form is closed, will there be errors if the background worker is in the middle of a task? - That depends on what the task is. If it is trying to update the UI which no longer exists then yes. If it was saving a file to the hard drive then probably not. There are way to stop this though. Search for articles about stopping background worker threads.
- Can I put time consuming code in a background worker on a timer so it does it every N seconds? But what happens if it is not finished when it is called again? - I don't see why you couldn't do this, as far as what happens that again depends largely on what the BG Worker does. Multiple runnings can prevented several ways.
- Why can I not put this code in a background worker? - You are getting that error because you cannot access UI elements from a different thread which is what a BG worker is. Also any other methods (functions and subs) you decide to call from the BG Worker will be in the same thread as the BG worker. However BG Workers have a ProgressChanged event that fires and you can alter UI elements in that event since it runs in the main UI thread. Any other methods you call from the ProgressChanged event will be run in the UI thread. It may also be possible to use Delegates from a BG Worker to access the main UI. I usually only use Delegate in non-BGWorker async threads but they may also work in BG Worker threads. I don't see why it wouldn't work, but I have never tried it.
Last edited by Maverickz; Apr 23rd, 2016 at 01:38 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|